Learn with best explanation

Coding tutorials and solutions

Collection List Mapping in Hibernate

For mapping collection like one-to-many, many-to-one or many-to-many in persistent class in hibernate we are required to declare collection type in the class from the following.

  • java.util.List
  • java.util.Set
  • java.util.SortedSet
  • java.util.Map
  • java.util.SortedMap
  • java.util.Collection
  • or write the implementation of org.hibernate.usertype.UserCollectionType

Collection Example List Mapping in Hibernate

In this example we will see complete working example of collection mapping by list.

Create persistent class

The collection should be defined in persistent like this.

Customer.java


public class Customer {

    private Integer customerId;
    private String customerName;
    private List<Orders> orders; // Collection type

    // Getters and Setters  
}

Orders.java



public class Orders {
    private Integer orderId;
    private String orderDetail;

    // Getters and Setters 

}

Mapping collection in mapping file in hibernate



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 5.3//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="entityonetomany">
    <class name="Customer">         <!--Class tag-->
        <id name="customerId">    
            <generator class="increment"></generator>
        </id>
        <property name="customerName" ></property>

        <list name="orders" cascade="all"> <!--Collection tag-->
            <key column="customer_frk_id"></key>
            <index column="type"></index>
            <one-to-many class="Orders"/>
        </list>
    </class>

</hibernate-mapping>>

Explaining mapping file in hibernate

Relation: one-to-many i.e Customer has many Orders
Persistent class: Customer, Order

There is DOCTYPE tag defined with spacified URLs for mapping configuration and then hibernate-mapping tag where location of persistent classes or package name is declared.

There is most important tag in mapping file called class tag which contains number of other tags like Id, Property, List tag.

  • <class> In Class Tag we define the name of the persistent class which has or doesn't have the relation in this cas has relation.Inside the Class Tag there are number of elements or other tags to be defiened like Id Tag which specifies the Id for the table and Property Tag which defines or created the other columns in the table.
  • <id> tag is used to define identfier in the table.
  • <property> tags is used to create columns in the table.
  • <List> makes the ralation and contains the name of persisitent class which has the relation.
  • <key> tag is used to define the foreign key based on Customer table identifier in the table which stores relational data in this case Order table.
  • <index> tag is used to identify the type. List and Map are indexed collection
  • <one-to-many> tag is used to define the relation with persistent class or the joined table which stores relational data

In the scenario above List is mapped by one-to-many relation. There are many orders for one customer.

one to many relation in hibernate javakar

Understanding key tag

The key tag is used to define the foreign key in the joined table based on the original identity. The foreign key tag is nullable by default. So for non-nullable foreign key we are required to specify not-null attribute such as:


<key column="qid" not-null="true" ></key>  

The other attributes of the key tag are column, on-delete, property-ref, not-null, update and unique.



<key column="customer_frk_id"
     not-null="true"
     on-delete="cascade"
     update="true"
     unique="false"
/>