Example On Composite Primary Keys In Hibernate
Composite primary keys means having more than one primary key, let us see few points on this concept
- If the table has a primary key then in the hibernate mapping file we need to configure that column by using <id /> element right..!
- Even though the database table doesn’t have any primary key, we must configure one column as id (one primary key is must)
- If the database table has more than one column as primary key then we call it as composite primary key, so if the table has multiple primary key columns , in order to configure these primary key columns in the hibernate mapping file we need to use one new element called <composite-id …..> </composite-id>
Example On this__
Files required….
- Product.java (Pojo)
- ForOurLogic.java (for our logic)
- hibernate.cfg.xml
- Product.hbm.xml
Product.java
12345678910111213141516171819202122232425262728293031323334353637package str;
public class Product implements java.io.Serializable{
private static final long serialVersionUID = 1L; private int productId; private String proName; private double price; public void setProductId(int productId) { this.productId = productId; } public int getProductId() { return productId; } public void setProName(String proName) { this.proName = proName; } public String getProName() { return proName; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } }
hibernate.cfg.xml
1234567891011121314151617181920<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver </property> <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">admin</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="Product.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Product.hbm.xml
1234567891011121314151617<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="str.Product" table="products">
<composite-id>
<key-property name="productId" column="pid" />
<key-property name="proName" column="pname" length="10" />
</composite-id>
<property name="price"/> </class> </hibernate-mapping>
ForOurLogic.java
1234567891011121314151617181920212223242526272829303132package str;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class ForOurLogic {
public static void main(String[] args)
{
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Product p=new Product();
p.setProductId(101);
p.setProName("iPhone");
p.setPrice(25000);
Transaction tx=session.beginTransaction(); session.save(p); System.out.println("Object Loaded successfully.....!!"); tx.commit(); session.close(); factory.close(); } }
Eclipse output
![]() |
![]() |
In the database
![]() |
![]() |
Notes:
- see Product.java pojo class, in line number 3 i have implemented the
java.io.Serializableinterface, this is the first time am writing this implementation for the pojo class right…! we will see the reason why we use this serializable interface later. - But remember, if we want to use the composite primary keys we must implement our pojo class with Serializable interface
- hibernate.cfg.xml is normal as previous programs, something like hello world program
- come to Product.hbm.xml, see line number 9–12, this time we are using one new element<composite-id>
- Actually if we have a single primary key, we need to use <id> element, but this time we have multiple primary keys, so we need to use this new element <composite-id>
- Actually we will see the exact concept of this composite primary keys in the next example (loading an object with composite key)





post a comment