About Me

My photo
"Enoughtheory.com" had its humble beginning in the year 2011 by ( Founder of Enoughtheory.com ) Mr Ravi Kant Soni , an Enterprise Java and Spring Framework Specialist, with a bachelor degree (B.E) in Information Science and Engineering from Reva Institute of Technology at Bangalore. He has been into the software development discipline for many years now. Ravi has worn many hats throughout his tenure, ranging from software development, designing multi-tenant applications, integration of new technology into an existing system, to his current love of writing a Spring Framework book. Currently, he is a lead engineer at HCL Technology. Ravi has focused on Web and Enterprise development using Spring Framework for most of his career and has been extensively involved in application design and implementation. He has developed applications for Core-Bank, HR and Payroll System, and e-Commerce systems using Spring Framework. Ravi Kant Soni is author of book "Learning Spring Application development" http://learningspringapplicationdevelopment.com

Tuesday 8 November 2011

Collection mappings - Collections


  • Collection mappings include OneToManyManyToMany,
  • JPA requires that the type of the collection field or get/set methods be one of the Java collection interfaces, CollectionListSet, or Map.

Collection Implementations

  • Your field should not be of a collection implementation type, such as ArrayList.

Duplicates

  • List in Java supports duplicate entries, and a Set does not.
  • In the database, duplicates are generally not supported.
  •  Technically it could be possible if a JoinTable is used, but JPA does not require duplicates to be supported, and most providers do not.
  • If you require duplicate support, you may need to create an object that represents and maps to the join table. This object would still require a unique Id, such as a GeneratedValue

Ordering

  • JPA allows the collection values to be ordered by the database when retrieved.
  • This is done through the @OrderBy annotation or <order-by> XML element.
  • The value of the OrderBy is a JPQL ORDER BY string. 
  • Attribute name followed by ASC or DESC for ascending or descending ordering. 
  • If no OrderByvalue is given it is assumed to be the Id of the target object.
  • The OrderBy value must be a mapped attribute of the target object.
  • Using an OrderBy does not ensure the collection is ordered in memory.You are responsible for adding to the collection in the correct order. 
  • Java does define a SortedSet interface and TreeSet collection implementation that does maintain an order.
  • You can also use the Collections.sort() method to sort a List when required. 

Example of a collection order by annotation

@Entity
public class Employee {
  @Id
  private long id;
  ...
  @OneToMany
  @OrderBy("areaCode")
  private List<Phone> phones;
  ...
}


The OrderColumn is maintained by the mapping and should not be an attribute of the target object. The table for the OrderColumn depends on the mapping. For a OneToMany mapping it will be in the target object's table. For aManyToMany mapping or a OneToMany using a JoinTable it will be in the join table. For an ElementCollection mapping it will be in the target table.

Example of a collection order column database

EMPLOYEE (table)
IDFIRSTNAMELASTNAMESALARY
1BobWay50000
2SarahSmith60000
EMPLOYEE_PHONE (table)
EMPLOYEE_IDPHONE_IDINDEX
110
131
220
241
PHONE(table)
IDAREACODENUMBER
1613792-7777
2416798-6666
3613792-9999
4416798-5555

Example of a collection order column annotation

@Entity
public class Employee {
  @Id
  private long id;
  ...
  @OneToMany
  @OrderColumn(name="INDEX")
  private List<Phone> phones;
  ...
}

No comments:

Post a Comment