Hibernate Collection Mapping
Hibernate supports collection mapping as value type. In Hibernate collection mapping, the collection are mapped into a separate table but are not exposed as entity on the Java side. Hibernate supports following collection interfaces as value type:- java.util.Set – java.util.HashSet is used to store value.
- java.util.SortedSet – java.util.TreeSet is used to store value.
- java.util.List – java.util.ArrayList is used to store value. Preserves the position with an index column
- Bag semantics – java.util.ArrayList is used to store valre however the position is not preserved.
- java.util.Map – java.util.HashMap is used to store value.
- java.util.SortedMap – java.util.TreeMap is used to store value.
Let’s say we want to store Phones for a Student. There are more than one Phone for a Student. So we will have two tables corresponding to Student and Phone. However we do not want to expose Phone
Table STUDENT
STUDENT_ID ...
Table PHONE
STUDENT_ID NUMBER ...
STUDENT_ID is the foreign key and PHONE table has no primary key. To map the Phone collection as a Set in the Student Entity class
Student class
... @org.hibernate.annotations.CollectionOfElements(targetElement = java.lang.String.class) @JoinTable( name="PHONE",joinColumns = @JoinColumn(name="STUDENT_ID")) @Column(name="PHONE_NO") public Set<String> getPhones() { ...
The lifecycle of Phone is tightly coupled to Student. Also Phone table is not exposed as an entity. If the Set need to be sorted
... @org.hibernate.annotations.CollectionOfElements(targetElement = java.lang.String.class) @JoinTable( name="PHONE",joinColumns = @JoinColumn(name="STUDENT_ID")) @Column(name="PHONE_NO") @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL) public Set<String> getPhones() { ...
A comparator can also be used for Sort type.
To map the Phone collection as a list
... @org.hibernate.annotations.CollectionOfElements @JoinTable( name="PHONE",joinColumns = @JoinColumn(name="STUDENT_ID")) @org.hibernate.annotations.IndexColumn(name=“INDEX_POSITION", base =1) @Column(name="PHONE_NO") public List<String> getPhones() { return phones; }
Here the index is mapped to a INDEX_POSITION column in the table and preserves the ordering. If the index is not given, this works like a Bag. Bag is a list but does not preserves the position.
To map Phone as a map where PHONE_NAME will act as key and PHONE_NO as value from the PHONE table. To map it
... @org.hibernate.annotations.CollectionOfElements @JoinTable( name="PHONE",joinColumns = @JoinColumn(name="STUDENT_ID")) @org.hibernate.annotations.MapKey(columns = @Column(name="PHONE_NAME")) @Column(name="PHONE_NO") public Map<String,String> getPhones() { ...
Here the Phone table is mapped directly as a collection. We can also expose Phone as a value object. In this case define Phone as a class
@Embeddable public class PhoneValue { protected Student student; protected String name; protected String phoneNumber; @org.hibernate.annotations.Parent public Student getStudent() { return student; } ...
Also in the above case we maintain a back pointer for Student so that we can navigate from Phone to Student. To define the collection of embedded object in Student class
... protected Collection<PhoneValue> phoneValues = new ArrayList<PhoneValue>(); @org.hibernate.annotations.CollectionOfElements @JoinTable(name = "Student_Phone_Value",joinColumns = @JoinColumn(name="STUDENT_ID")) @CollectionId(columns= @Column(name="STUDENT_PHONE_VALUE_ID"), type=@org.hibernate.annotations.Type(type="long"), generator="sequence") public Collection<PhoneValue> getPhoneValues() { return phoneValues; } ...
Hibernate collection mapping is a way of mapping the collection table as values. The lifecycle of the the collections are tightly bound to the collection of the owning entity.
Back to Hibernate Index
Back To Java Home
Back To Home
Sidebar
Last wiki comments
- Introduction to ORM: ugg boots
- Introduction to ORM: ugg boots
- AOP: Thanks
- Lalit Bhatt: Superb Collection
- Lalit Bhatt: J2EE training
- Introduction to ORM: timberland shoes
- Introduction to ORM: jordan shoes
- Introduction to ORM: nike air max
- Pune Tourist Spots: KONARK PARK CLOSED
- jQuery Form Validations: Jquery Developer
Sidebar
Random Pages
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Prospective MLA for Pune election - 2009: ugg boots
Wed 01 of Sep., 2010 12:58 IST
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
Post new comment