Composite Primary Key In Hibernate With Select Query

Composite primary keys means having more than one primary key right..?

Example On this__

Files required….

  • Product.java (Pojo)
  • ForOurLogic4Load.java (for our logic)
  • hibernate.cfg.xml
  • Product.hbm.xml

All files are same like previous program, but ForOurLogic4Load.java is the new file for loading an object from the database

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="price" />

</composite-id>

<property name="proName" column="pname" length="10"/> </class> </hibernate-mapping>

 

ForOurLogic4Load.java

12345678910111213141516171819202122232425262728293031323334package str;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class ForOurLogic4Load { 

	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.setPrice(25000);

Object o=session.get(Product.class, p);

// here p must be an serializable object, Product p1=(Product)o; System.out.println("The price is: "+p1.getProName()); System.out.println("Object Loaded successfully.....!!"); session.close(); factory.close(); } 

Notes:

    • regarding ForOurLogic4Load.java actually for loading an object we have to pass the primary key column value like…
1Object o=session.get(Product.class, new Integer(101));
    • Actually that is the case with single primary key, but see in this example we are using multiple primary keys (2 primary key values), so while loading an object how we will give two values….?
1Object o=session.get(Product.class, new Integer(101,25000));
  • ? which is absolutely wrong……………!!!!!!!!!
  • so how we can pass the multiple primary key values for loading that object.. ?
      • First we need to set the values just like in line numbers 19,20 , ensure you are setting the values which already have in the database for that particular object.
      • Then we need to pass that object as the parameter to load the object.
    1Object o=session.get(Product.class, p);
      • What am saying is that the second parameter of the get method is always a Serializable object, so in the Product.java line number 3 i just implemented with java.io.Serializable
      • Remember, if we have single primary key we used to give
    1Object o=session.get(Product.class, new Integer(101));

    here new Integer(101) is the wrapper, all wrappers are Serializable by default.

Related Articles

post a comment