Thursday, April 21, 2016

Hibernate One to Many Relationship with Example

One to may relationship can occur between two tables and where one table row can have multiple matching rows from other table. In hibernate also same concept with the name of Entity.

How its occurring ?

As usual, by the help of primary key Foreign key relationship.

One To Many (XML Mapping)

We have taken two model classes to describing this relationship. Item class and ItemRecord class.

Item.java 


package com.javahit.items;

import java.util.HashSet;
import java.util.Set;

public class Item implements java.io.Serializable {

 private Integer itemId;
 private String itemCode;
 private String itemName;


 private Set<itemrecord> itemRecords = 
    new HashSet<itemrecord>(0);

 //getter setter
}

ItemRecord.java

package com.javahit.items;

import java.util.Date;

public class ItemRecord implements java.io.Serializable {

 private Integer itemRecordId;
 private Item item;
 private Float price;
 private Long volume;


 //getter & setter
}

Hibernate XML Mapping file

For the above two model classes we should create hibernate mapping files.Lets see , how its look like

item.hbm.xml


    
        
            
            
        
        
            
        
        
            
        
        
            
                
            
            
        
    

inverse = true specifies, which side the relationship(One to Many , Many to Many) should take care. Mostly it will be come with these two relationship only.

lazy specifies, whether to load child objects(tables) or not, while loading parent object.(tables).

fetch = "select" - do a lazy load for all collection and entities. Remaining possible strategies are below.
fetch = "join" - It will disable lazy load , so all collection and entities will be loaded always.
fetch = "batch-size" - You can mention batch size here to fetch collection and entity data.
fectch = "subselect" - It will make your collection group into a subselect.

itemRecord.hbm.xml


    
        
            
            
        
        
            
        
        
            
        

        
            
        
        
    

Hibernate Configuration xml

Now configure your two model classes hbm xml as well as db driver, userName, pwd etc details.



    com.mysql.jdbc.Driver
    jdbc:mysql://localhost:3310/testdb
    sa
    password
    org.hibernate.dialect.MySQLDialect
    true
    true
    
    



Store Data

This is our final java class file which contains hibernate configuration logic and one to many working code.

package com.javahit;

import java.util.Date;
import org.hibernate.Session;
import com.javahit.items.Item;
import com.javahit.items.ItemRecord;


public class StoreAndRetreive {
 public static void main(String[] args) {
 
 Configuration cfg=new Configuration();    
        cfg.configure("hibernate.cfg.xml");  
    
        SessionFactory sf=cfg.buildSessionFactory();    
        Session session=sf.openSession();    
        Transaction tx=session.beginTransaction();

 Item item = new Item();
        item.setItemCode("1001");
        item.setItemName("Item Name");
        session.save(item);
        
        ItemRecord itemRecords = new ItemRecord();
        itemRecords.setPrice(new Float("5.2"));
        itemRecords.setVolume(10L);
        
        itemRecords.setItem(item);        
        item.getItemRecords().add(itemRecords);

        session.save(itemRecords);

 tx.commit();
 System.out.println("Sucess");
 }
}

No comments:

Post a Comment