One to One RelationShip in Hibernate:--------------
================================================================================
//Student.java
//StudentClass.java
//student.hbm.xml
//studentclass.hbm.xml
//hibernate.cfg.xml
//SessionUtility.java
//HibernateTest .java
//DatabaseOperation.java
==========================================================================================
step:--
Create the java project
Add jar files for hibernate
Create the Persistent class
Create the mapping file for Persistent class
Create the Configuration file
Create the class that retrieves or stores the persistent object
Run the application
==========================================================================================
jar file required........
antlr-2.7.5H3.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jta.jar
postgresql-9.3-1103.jdbc3.jar
===============================================================================================
==============================================================================
//Student.java
package govind;
public class Student {
private int sid;
private String fname;
private String mname;
private String lname;
private String rollno;
private int age;
private StudentClass studentclass;
public Student()
{
}
public Student(String fname, String mname, String lname, String rollno, int age,
StudentClass studentclass) {
super();
this.fname = fname;
this.mname = mname;
this.lname = lname;
this.rollno = rollno;
this.age = age;
this.studentclass = studentclass;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getMname() {
return mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getRollno() {
return rollno;
}
public void setRollno(String rollno) {
this.rollno = rollno;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public StudentClass getStudentclass() {
return studentclass;
}
public void setStudentclass(StudentClass studentclass) {
this.studentclass = studentclass;
}
}
===============================================================================
//StudentClass.java
package govind;
public class StudentClass {
private int sid;
private String classid;
private String classname;
private Student student;
public StudentClass()
{
super();
}
public StudentClass(String classid, String classname) {
super();
this.classid = classid;
this.classname = classname;
}
public StudentClass(String classid, String classname, Student student) {
super();
this.classid = classid;
this.classname = classname;
this.student = student;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getClassid() {
return classid;
}
public void setClassid(String classid) {
this.classid = classid;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
===============================================================================
//student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="govind.Student" table="Student_info">
<id name="sid" type="int" column="sid">
<generator class="increment"></generator>
</id>
<property name="fname" column="fname" type="string"/>
<property name="mname" column="mname" type="string"/>
<property name="lname" column="lname" type="string"/>
<property name="rollno" column="rollno" type="string"/>
<property name="age" column="age" type="int"/>
<one-to-one name="studentclass"
class="govind.StudentClass" cascade="all">
</one-to-one>
</class>
</hibernate-mapping>
============================================================================
//studentclass.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="govind.StudentClass" table="Stu_Class">
<id name="sid" type="int" column="sid">
<generator class="foreign">
<param name="property">student</param>
</generator>
</id>
<one-to-one name="student" class="govind.Student" constrained="true">
</one-to-one>
<property name="classid" column="classid" type="string"></property>
<property name="classname" column="classname" type="string"></property>
</class>
</hibernate-mapping>
===============================================================================
//hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5433/java</property>
<property name="connection.username">postgres</property>
<property name="connection.password">manager</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">30</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="govind/student.hbm.xml"/>
<mapping resource="govind/studentclass.hbm.xml"/>
</session-factory>
</hibernate-configuration>
===============================================================================
//SessionUtility.java
package govind;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionUtility {
static SessionFactory sf=null;
static
{
try
{
sf = new Configuration().configure("/govind/hibernate.cfg.xml").buildSessionFactory();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory()
{
return sf;
}
}
=============================================================================
// DatabaseOperation.java
package govind;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class DatabaseOperation {
public Integer addStudent(Student student)
{
Transaction tx=null;
Integer sid=null;
SessionFactory sf = SessionUtility.getSessionFactory();
Session session = sf.openSession();
try
{
tx=session.beginTransaction();
sid=(Integer)session.save(student);
tx.commit();
}
catch (HibernateException e) {
if(tx!=null){
tx.rollback();
}
e.printStackTrace();
}
finally {
session.close();
}
return sid;
}
public void showAllData()
{
Transaction tx=null;
SessionFactory sf = SessionUtility.getSessionFactory();
Session session=sf.openSession();
try
{
tx=session.beginTransaction();
Query query = session.createQuery("from Student");
List<Student> l = query.list();
for(Student s:l)
{
System.out.println("Student Id="+s.getSid());
System.out.println("First Name="+s.getFname());
System.out.println("Middle Name="+s.getMname());
System.out.println("Last Name="+s.getLname());
System.out.println("Roll Number="+s.getRollno());
StudentClass s1 = s.getStudentclass();
System.out.println("Class Id="+s1.getClassid());
System.out.println("Class Name="+s1.getClassname());
}
tx.commit();
}
catch (HibernateException e) {
if(tx!=null)
{
tx.rollback();
}
e.printStackTrace();
}
finally
{
session.close();
}
}
}
===============================================================================
//HibernateTest.java
package govind;
public class HibernateTest {
public static void main(String[] args) {
StudentClass sc1=new StudentClass("0101mca", "mca");
StudentClass sc2=new StudentClass("0102bca", "bca");
StudentClass sc3=new StudentClass("0103bba", "bba");
StudentClass sc4=new StudentClass("0104mtech", "Mtech");
StudentClass sc5=new StudentClass("0105btech", "btech");
Student s1=new Student("rajesh","kumar","singh","01013mca01",24,sc1);
Student s2=new Student("sanjay","kumar","jha","0102bca02",25,sc2);
Student s3=new Student("ajit","kumar","singh","0103bba03",26,sc3);
Student s4=new Student("govind","ballabh","khan","0104mtech04",27,sc4);
Student s5=new Student("murari","kumar","chy","0105btech05",18,sc5);
sc1.setStudent(s1);
sc2.setStudent(s2);
sc3.setStudent(s3);
sc4.setStudent(s4);
sc5.setStudent(s5);
DatabaseOperation d=new DatabaseOperation();
d.addStudent(s1);
d.addStudent(s2);
d.addStudent(s3);
d.addStudent(s4);
d.addStudent(s5);
d.showAllData();
}
}
==============================================================================
o/p view:-
Hibernate: select max(sid) from Student_info
Hibernate: insert into Student_info (fname, mname, lname, rollno, age, sid) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Stu_Class (classid, classname, sid) values (?, ?, ?)
Hibernate: insert into Student_info (fname, mname, lname, rollno, age, sid) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Stu_Class (classid, classname, sid) values (?, ?, ?)
Hibernate: insert into Student_info (fname, mname, lname, rollno, age, sid) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Stu_Class (classid, classname, sid) values (?, ?, ?)
Hibernate: insert into Student_info (fname, mname, lname, rollno, age, sid) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Stu_Class (classid, classname, sid) values (?, ?, ?)
Hibernate: insert into Student_info (fname, mname, lname, rollno, age, sid) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into Stu_Class (classid, classname, sid) values (?, ?, ?)
Hibernate: select student0_.sid as sid0_, student0_.fname as fname0_, student0_.mname as mname0_, student0_.lname as lname0_, student0_.rollno as rollno0_, student0_.age as age0_ from Student_info student0_
Hibernate: select studentcla0_.sid as sid1_0_, studentcla0_.classid as classid1_0_, studentcla0_.classname as classname1_0_ from Stu_Class studentcla0_ where studentcla0_.sid=?
Hibernate: select studentcla0_.sid as sid1_0_, studentcla0_.classid as classid1_0_, studentcla0_.classname as classname1_0_ from Stu_Class studentcla0_ where studentcla0_.sid=?
Hibernate: select studentcla0_.sid as sid1_0_, studentcla0_.classid as classid1_0_, studentcla0_.classname as classname1_0_ from Stu_Class studentcla0_ where studentcla0_.sid=?
Hibernate: select studentcla0_.sid as sid1_0_, studentcla0_.classid as classid1_0_, studentcla0_.classname as classname1_0_ from Stu_Class studentcla0_ where studentcla0_.sid=?
Hibernate: select studentcla0_.sid as sid1_0_, studentcla0_.classid as classid1_0_, studentcla0_.classname as classname1_0_ from Stu_Class studentcla0_ where studentcla0_.sid=?
Student Id=1
First Name=rajesh
Middle Name=kumar
Last Name=singh
Roll Number=01013mca01
Class Id=0101mca
Class Name=mca
Student Id=2
First Name=sanjay
Middle Name=kumar
Last Name=jha
Roll Number=0102bca02
Class Id=0102bca
Class Name=bca
Student Id=3
First Name=ajit
Middle Name=kumar
Last Name=singh
Roll Number=0103bba03
Class Id=0103bba
Class Name=bba
Student Id=4
First Name=govind
Middle Name=ballabh
Last Name=khan
Roll Number=0104mtech04
Class Id=0104mtech
Class Name=Mtech
Student Id=5
First Name=murari
Middle Name=kumar
Last Name=chy
Roll Number=0105btech05
Class Id=0105btech
Class Name=btech
project directory:-----------------------
Created Table In Database................................