Making Composite Key Through Hibernate

Posted By : Rahul Singh | 17-Sep-2017

When we want to retrieve and inserted data through the Composite key in hibernate then the class must be implemented by a Serializable interface.

 

Syntax

            
               
                  

Example:

 

1). Product.java - Create Bean or Pojo class for Product 

 

package product;
import java.io.Serializable;

public class ProductBean implements Serializable
{
   private int productId;
   private String productName;
   private double price;
   
   public int getProductId()
   {
      return productId;
   }
   public void setProductId( int productId )
   {
      this.productId = productId;
   }
   public String getProductName()
   {
      return productName;
   }
   public void setProductName( String productName )
   {
      this.productName = productName;
   }
   public double getPrice()
   {
      return price;
   }
   public void setPrice( double price )
   {
      this.price = price;
   }
}

 

 

2). product.hbm.XML- Map Pojo class with database table's column via hibernate mapping

 
<?xml version="1.0" encoding="UTF-8"?>
 
<hibernate-mapping>
<class name="product.ProductBean" table="products" >
<composite-id>
<key-property name="productId" column="productId" />
<key-property name="productName" column="productName" />
</composite-id>
<property name="price" /> 
</class>
</hibernate-mapping>
 

 

3). hibernate.cfg.xml - Configure database access entry in .cfg file and map .hbm file  

 

<DTD--->

<hibernate-configuration>  
  
    <session-factory>  
        <property name="hbm2ddl.auto">update</property>  
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>  
        <property name="connection.username">system</property>  
        <property name="connection.password">oracle</property>  
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
    <mapping resource="product.hbm.xml"/>  
    </session-factory>  
  
</hibernate-configuration> 

 

 

4). StoreProductData.java - Add product data via bean class into database using hibernate APIs 

package product;
import org.hibernate.*;
import org.hibernate.cfg.*;
 
public class StoreProductData
{
public static void main(String arg[])
{
Session session=new Configuration().configure("hibernate.cfg.xml")  
                    .buildSessionFactory().openSession();  
 
ProductBean product1 = new ProductBean();
product1.setProductId( 101 );
product1.setProductName( "LCD" );
product1.setPrice( 25000 );
   
ProductBean product2 = new ProductBean();
product2.setProductId( 102 );
product2.setProductName( "AC" );
product2.setPrice( 55000 );
  
Transaction tx=session.beginTransaction();
session.save(product1);
session.save(product2);
tx.commit();
System.out.println("Product Data Saved Successfully");
session.close();
}
}
 
 
 
 
5). RetrieveProductData.java - Access data from Database using of composite key in hibernate APIs 
package product;
import org.hibernate.*;
import org.hibernate.cfg.*;
 
public class RetrieveProductData
{
public static void main(String arg[])
{
Session session=new Configuration().configure("product.cfg.xml")  
                    .buildSessionFactory().openSession();  
 
ProductBean product1 = new ProductBean();
product1.setProductId( 101 );
product1.setProductName( "LCD" );
 
//here product1 must be Serializable object
ProductBean pp1=(ProductBean)session.get(ProductBean.class,product1);
 
System.out.println(pp1.getPrice() +" Data loaded"); //data get successfully
session.close();
}
}

About Author

Author Image
Rahul Singh

Rahul singh is a Java Developer and having experience in developing Applications. He is a quick learner.

Request for Proposal

Name is required

Comment is required

Sending message..