Hibernate

adplus-dvertising
Working with Collection
Previous Home Next
Collections Mappings:

If an entity or class has collection of values for a particular variable, then we can map those values using any one of the collection interfaces available in java.

Collection Mapping shows that when an object or class have the collection of values for a perticular variable, then we map those values using any one of the collection interfaces available in java.Hibernate can persist instances of java.util.Map, java.util.Set, java.util.SortedMap, java.util.SortedSet, java.util.List, and any array of persistent entities or values.

Arrays are supported by Hibernate with for Java primitive value types and for everything else. However, they are rarely used so I'm not going to discuss them in this tutorial.

If you want to map a user defined collection interfaces which is not directly supported by Hibernate, you need to tell Hibernate about the semantics of your custom collections which is not very easy and not recommend to be used.

It is very much possible that an Entity class can have a reference to another class as a member variable. If the referred class does not have it's own life cycle and completely depends on the life cycle of the owning entity class, then the referred class hence therefore is called as the Component class.

The mapping of Collection of Components is also possible in a similar way just as the mapping of regular Collections with minor configuration differences. We will see these two mappings in detail with examples. Mapping for a class having a reference to another class as a member variable.

Example of Collection

In the below example given class has a collection of child instances that for try to understand: Example-1. Example classes Parent and Child


public class Parent 
          {
             private long id;
             private Set<Child> children;

             // getter/setter
            ...
           }


        public class Child 
        {
            private long id;
            private String name

   
            // getter/setter
            ...
          }

In the above example if each child has, at most, at parent the most natural mapping is a one to one mapping shows If each child has, at most, one parent, the most natural mapping is a one-to-many association:

Example 2. One to many unidirectional Parent-Child relationship using annotations


public class Parent {
    @Id
    @GeneratedValue
    private long id;

    @OneToMany
    private Set<Child> children;

    // getter/setter
    ...
}


public class Child {
   @Id
   @GeneratedValue
   private long id;
   private String name;

   
   // getter/setter
   ...
}

Example 3. One to many unidirectional Parent-Child relationship using mapping files


  <hibernate-mapping>

    <class name="Parent">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <set name="children">
            <key column="parent_id"/>
            <one-to-many class="Child"/>
        </set>
    </class>

    <class name="Child">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <property name="name"/>
    </class>

</hibernate-mapping>

This maps to the following table definitions: Example 4. Table definitions for unidirectional Parent-Child relationship


          create table parent ( id bigint not null primary key )
          create table child ( id bigint not null primary key, name varchar(255), parent_id bigint )
          alter table child add constraint childfk0 (parent_id) references parent

If the parent is required, use a bidirectional one-to-many association:

Example 5. One to many bidirectional Parent-Child relationship using annotations


public class Parent {
    @Id
    @GeneratedValue
    private long id;

    @OneToMany(mappedBy="parent")
    private Set<Child> children;

    // getter/setter
    ...
}


public class Child {
   @Id
   @GeneratedValue
   private long id;

   private String name;
 
   @ManyToOne
   private Parent parent;

   
   // getter/setter
   ...
}  

Example 6. One to many bidirectional Parent-Child relationship using mapping files


<hibernate-mapping>

    <class name="Parent">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <set name="children" inverse="true">
            <key column="parent_id"/>
            <one-to-many class="Child"/>
        </set>
    </class>

    <class name="Child">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <property name="name"/>
        <many-to-one name="parent" class="Parent" column="parent_id" not-null="true"/>
    </class>

</hibernate-mapping>

Notice the NOT NULL constraint:

Example 7. Table definitions for bidirectional Parent-Child relationship


create table parent ( id bigint not null primary key )
create table child ( id bigint not null
                     primary key,
                     name varchar(255),
                     parent_id bigint not null )
alter table child add constraint childfk0 (parent_id) references parent 

Alternatively, if this association must be unidirectional you can enforce the NOT NULL constraint.

Example 8. Enforcing NOT NULL constraint in unidirectional relation using annotations


public class Parent {
    @Id
    @GeneratedValue
    private long id;

    @OneToMany(optional=false)
    private Set<Child> children;

    // getter/setter
    ...
}


public class Child {
   @Id
   @GeneratedValue
   private long id;
   private String name;

   
   // getter/setter
   ...
}  

Example 9. Enforcing NOT NULL constraint in unidirectional relation using mapping files


<hibernate-mapping>

    <class name="Parent">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <set name="children">
            <key column="parent_id" not-null="true"/>
            <one-to-many class="Child"/>
        </set>
    </class>

    <class name="Child">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <property name="name"/>
    </class>

</hibernate-mapping>

On the other hand, if a child has multiple parents, a many-to-many association is appropriate.

Example 10. Many to many Parent-Child relationship using annotations


public class Parent {
    @Id
    @GeneratedValue
    private long id;

    @ManyToMany
    private Set<Child> children;

    // getter/setter
    ...
}


public class Child {
   @Id
   @GeneratedValue
   private long id;

   private String name;

   
   // getter/setter
   ...
}

Example 11. Many to many Parent-Child relationship using mapping files


<hibernate-mapping>

    <class name="Parent">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <set name="children" table="childset">
            <key column="parent_id"/>
            <many-to-many class="Child" column="child_id"/>
        </set>
    </class>

    <class name="Child">
        <id name="id">
            <generator class="sequence"/>
        </id>
        <property name="name"/>
    </class>

</hibernate-mapping>

Table definitions:

Example 12. Table definitions for many to many releationship


create table parent ( id bigint not null primary key )
create table child ( id bigint not null primary key, name varchar(255) )
create table childset ( parent_id bigint not null,
                        child_id bigint not null,
                        primary key ( parent_id, child_id ) )
alter table childset add constraint childsetfk0 (parent_id) references parent
alter table childset add constraint childsetfk1 (child_id) references child 

Previous Home Next