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
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
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahul Singh
Rahul singh is a Java Developer and having experience in developing Applications. He is a quick learner.