Sunday, April 10, 2011

HIbernate composite Primary Key

Strategy:
We have to create a separate class to represent the composite primary key and the fields that represent the primary key has to be moved to the new class.

So suppose We have a class called as Contact1 with following fields
ContactId // primary key field
EmailId // primary key fields
firstName
and LastName

So we will create a new class called ContactPK with two fields
ContactId // primary key field
EmailId // primary key fields

and Our Contact1 class will have following fields
firstName
and LastName

rest all is just simple.

We will look at how to do select operation and insert operation here.

Code for Contact1 is as follows:

package blog.javaespresso.tut.hibernate.compositekey;

import java.io.Serializable;

public class ContactPK implements Serializable{
private String email;
private long id;

public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}

}


Code for ContactPK class:


package blog.javaespresso.tut.hibernate.compositekey;

public class Contact1 {
private String firstName;
private String lastName;
private ContactPK contactPK;

public ContactPK getContactPK() {
return contactPK;
}

public void setContactPK(ContactPK contactPK) {
this.contactPK = contactPK;
}

/**
* @return First Name
*/
public String getFirstName() {
return firstName;
}

/**
* @return Last name
*/
public String getLastName() {
return lastName;
}


/**
* @param string
* Sets the First Name
*/
public void setFirstName(String string) {
firstName = string;
}

/**
* @param string
* sets the Last Name
*/
public void setLastName(String string) {
lastName = string;
}
}


Hibernate mapping file (Contact1.hbm.xml)

<?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="blog.javaespresso.tut.hibernate.compositekey.Contact1" table="CONTACT1">
<composite-id name="contactPK"
class="blog.javaespresso.tut.hibernate.compositekey.ContactPK">
<key-property name="id" column="ID" type="long">
</key-property>
<key-property name="email" column="EMAIL" type="string">
</key-property>
</composite-id>
<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME" />
</property>
</class>
</hibernate-mapping>


Hibernate config file (Hibernate.cfg.xml): add the mapping file entry

<mapping resource="blog/javaespresso/tut/hibernate/compositekey/contact1.hbm.xml" />


Below is the structure of the project :




Class code to test the insert operation:


package blog.javaespresso.tut.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import blog.javaespresso.tut.hibernate.compositekey.*;
public class TestInsertCompositeKey {
public static void main(String[] args) {
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Contact1 contact = new Contact1();
ContactPK contactPK = new ContactPK();
contactPK.setEmail("holyshit@yahoo.com");
contactPK.setId(1);
contact.setContactPK(contactPK);
contact.setFirstName("holy");
contact.setLastName("shit");
Transaction transaction = session.beginTransaction();
session.save(contact);
transaction.commit();
System.out.println("Inserting Done!");
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}finally{
session.flush();
session.close();
}
}
}


Class code to test the select operation:


package blog.javaespresso.tut.hibernate.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import blog.javaespresso.tut.hibernate.compositekey.Contact1;
import blog.javaespresso.tut.hibernate.compositekey.ContactPK;
public class TestSelectComposite {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();
System.out.println("ftching Record");
ContactPK contactPK = new ContactPK();
contactPK.setEmail("deepak_38@yahoo.com");
contactPK.setId(1);
Contact1 contact1 = new Contact1();
session.load(contact1, contactPK);
System.out.println(contact1.getFirstName());
System.out.println(contact1.getLastName());
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
} finally {
session.close();http://www.blogger.com/img/blank.gif

}
http://www.blogger.com/img/blank.gif
}
}


Links to eclipse source code and the library files:
sourceCode



Library Files

2 comments: