jobs4timesLogo jobs4timesLogo

Associations

Agenda

Associations :

One to Many mapping :

  • One pojo class is related with multiple Pojo classes.
  • In parent pojo class we need to take one collection property. In the mapping file of parent pojo class we need to configure that collection details. Collection can be Set, List, or Map.
  • While mapping collection details, we need to add cascade attribute to transfer the operations on parent object to child objects.
  • Default value for cascade is none, which means even though relationship is exist, the operations we are doing on parent object will not transfer to child object.
  • In child class mapping file we need to take <key> tag to configure foreign key column.
  • Consider 2 pojo classes, Student and Course. For both pojo classes we need to take a separate mapping file. Configure these 2 mapping file details in hibernate.cfg.xml file.
Ex :
  • Here one student can join multiple courses. In Student class we need to take one collection property for courses and we need to configure these collection details inside Student.hbm.xml file.
  • In client application, set multiple courses to student object. Finally save student class object, then automatically course class objects also will be stored into the database.
Example : Student.java
package com.beans;

import java.util.Set;

public class Student {
private int studentId;
private String stuentName;
private Set courses;

public int getStudentId() {
	return studentId;
}
public void setStudentId(int studentId) {
	this.studentId = studentId;
}
public String getStuentName() {
	return stuentName;
}
public void setStuentName(String stuentName) {
	this.stuentName = stuentName;
}
public Set getCourses() {
	return courses;
}
public void setCourses(Set courses) {
	this.courses = courses;
}

}
Course.java
package com.beans;

public class Course {
private int courseId;
private String courseName;

public int getCourseId() {
	return courseId;
}
public void setCourseId(int courseId) {
	this.courseId = courseId;
}
public String getCourseName() {
	return courseName;
}
public void setCourseName(String courseName) {
	this.courseName = courseName;
}

}
Student.hbm.xml
<hibernate-mapping>

<class name="com.beans.Student" table="STUDENTDB">
  <id name="studentId" column="SID"/>
  <property name="stuentName" column="SNAME" length="15"/>
  <set name="courses" cascade="all">
   <key column="refSID"/>
   <one-to-many class="com.beans.Course"/>
  </set>      
</class>

</hibernate-mapping>
Course.hbm.cml
<hibernate-mapping>

<class name="com.beans.Course" table="COURSEDB">
  <id name="courseId" column="CID"/>
  <property name="courseName" column="CNAME" length="15"/>     
</class>

</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>

<session-factory>

<!-- Related to the connection properties -->

<property name="myeclipse.connection.profile">myJdbcDriver</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">lms</property>
<property name="connection.password">scott</property>


<!-- Related to the hibernate properties -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<!-- Related to hibernate mapping -->
<mapping resource="com/hiber/Student.hbm.xml" />
<mapping resource="com/hiber/Course.hbm.xml" />

</session-factory>

</hibernate-configuration>
Client Application 1 :
package com.client;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=new Student();
student.setStudentId(1);
student.setStuentName("Ashok");

Course course=new Course();
course.setCourseId(21);
course.setCourseName("hibernate");

Course course2=new Course();
course2.setCourseId(22);
course2.setCourseName("spring");

Set set=new HashSet();
set.add(course);
set.add(course2);

student.setCourses(set);

session.save(student);
tx.commit();

System.out.println("success");
}

}
Client Application 2 :
Note :
package com.client;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();

Student student=(Student)session.get(Student.class, 1);
System.out.println(student.getStudentId());
System.out.println(student.getStuentName());

Set set=student.getCourses();
Iterator iterator=set.iterator();

while(iterator.hasNext()){
Course course=(Course)iterator.next();
System.out.println(course.getCourseId());
System.out.println(course.getCourseName());
}

System.out.println("success");
}

}
Client Application 3 :
package com.client;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();

String hqlQuery="from Student";
Query query=session.createQuery(hqlQuery);

List list=query.list();
Iterator iterator=list.iterator();

while(iterator.hasNext()){
Student student=(Student)iterator.next();
System.out.println(student.getStudentId());
System.out.println(student.getStuentName());

Set set=student.getCourses();
Iterator iterator2=set.iterator();
 while(iterator2.hasNext()){
 Course course=(Course)iterator2.next();
 System.out.println(course.getCourseId());
 System.out.println(course.getCourseName());
 }
}

System.out.println("success");
}

}
Client Application 4 :
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=(Student)session.get(Student.class, 1);
session.delete(student);
tx.commit();
System.out.println("success");
}

}
Client Application 5 :
package com.client;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

String hqlQuery="from Student";
Query query=session.createQuery(hqlQuery);

List list=query.list();
Iterator iterator=list.iterator();

while(iterator.hasNext()){
Student student=(Student)iterator.next();
session.delete(student);
}
tx.commit();
System.out.println("success");
}

}

Many to One mapping :

  • In many to one mapping, the relationship is from child class to parent class.
  • In child pojo class, we need to take one parent type property.
  • In child class mapping file, configure this parent type property with <many-to-one> tag.
  • In client application, set parent class object to child class object and finally save child class object. Then automatically parent class object also will be stored into the database.
Example :
Student.java
package com.beans;

public class Student {
private int studentId;
private String stuentName;

public int getStudentId() {
	return studentId;
}
public void setStudentId(int studentId) {
	this.studentId = studentId;
}
public String getStuentName() {
	return stuentName;
}
public void setStuentName(String stuentName) {
	this.stuentName = stuentName;
}

}
Course.java
package com.beans;

public class Course {
private int courseId;
private String courseName;

private Student student;

public int getCourseId() {
	return courseId;
}
public void setCourseId(int courseId) {
	this.courseId = courseId;
}
public String getCourseName() {
	return courseName;
}
public void setCourseName(String courseName) {
	this.courseName = courseName;
}
public Student getStudent() {
	return student;
}
public void setStudent(Student student) {
	this.student = student;
}

}
Student.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Student" table="STUDENTDB">
  <id name="studentId" column="SID"/>
  <property name="stuentName" column="SNAME" length="15"/>       
</class>

</hibernate-mapping>
Course.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Course" table="COURSEDB">
  <id name="courseId" column="CID"/>
  <property name="courseName" column="CNAME" length="15"/> 
  
  <many-to-one name="student" column="refSID" 
                   class="com.beans.Student" cascade="all"/>    
</class>

</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>

<session-factory>

<!-- Related to the connection properties -->

<property name="myeclipse.connection.profile">myJdbcDriver</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">lms</property>
<property name="connection.password">scott</property>


<!-- Related to the hibernate properties -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<!-- Related to hibernate mapping -->
<mapping resource="com/hiber/Student.hbm.xml" />
<mapping resource="com/hiber/Course.hbm.xml" />

</session-factory>

</hibernate-configuration>
Client Application 1 :
ClientApp.java
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=new Student();
student.setStudentId(101);
student.setStuentName("Arun");

Course course=new Course();
course.setCourseId(201);
course.setCourseName("hibernate");
course.setStudent(student);

Course course2=new Course();
course2.setCourseId(202);
course2.setCourseName("spring");
course2.setStudent(student);

session.save(course);
session.save(course2);

tx.commit();
System.out.println("success");
}

}
Client Application 2 :
ClientApp.java
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Course course=(Course)session.get(Course.class, 201);
System.out.println(course.getCourseId());
System.out.println(course.getCourseName());

Student student=course.getStudent();
System.out.println(student.getStudentId());
System.out.println(student.getStuentName());

tx.commit();
System.out.println("success");
}

}
Client Application 3 :
ClientApp.java
package com.client;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

String hqlQuery="from Course";
Query query=session.createQuery(hqlQuery);
List list=query.list();
Iterator iterator=list.iterator();

while(iterator.hasNext()){
Course course=(Course)iterator.next();
System.out.println(course.getCourseId());
System.out.println(course.getCourseName());

Student student=course.getStudent();
System.out.println(student.getStudentId());
System.out.println(student.getStuentName());
}

System.out.println("success");
}

}
Client Application 4 :
ClientApp.java
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Course course=(Course)session.get(Course.class, 201);
session.delete(course);
tx.commit();

System.out.println("success");
}

}

One to Many Bidirectional :

Applying both one to many and many to one is nothing but one to many bidirectional.

Example :

Student.java
package com.beans;

import java.util.Set;

public class Student {
private int studentId;
private String stuentName;

private Set courses;

public int getStudentId() {
	return studentId;
}
public void setStudentId(int studentId) {
	this.studentId = studentId;
}
public String getStuentName() {
	return stuentName;
}
public void setStuentName(String stuentName) {
	this.stuentName = stuentName;
}
public Set getCourses() {
	return courses;
}
public void setCourses(Set courses) {
	this.courses = courses;
}

}

Course.java
package com.beans;

public class Course {
private int courseId;
private String courseName;

private Student student;

public int getCourseId() {
	return courseId;
}
public void setCourseId(int courseId) {
	this.courseId = courseId;
}
public String getCourseName() {
	return courseName;
}
public void setCourseName(String courseName) {
	this.courseName = courseName;
}
public Student getStudent() {
	return student;
}
public void setStudent(Student student) {
	this.student = student;
}

}
Student.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Student" table="STUDENTDB">
  <id name="studentId" column="SID"/>
  <property name="stuentName" column="SNAME" length="15"/> 
  
  <set name="courses" cascade="all" inverse="true">
   <key column="refSID"/>
   <one-to-many class="com.beans.Course"/>
  </set>
        
</class>

</hibernate-mapping>
Course.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Course" table="COURSEDB">
  <id name="courseId" column="CID"/>
  <property name="courseName" column="CNAME" length="15"/> 
  
  <many-to-one name="student" column="refCID" 
                   class="com.beans.Student" cascade="all"/>    
</class>

</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>

<session-factory>

<!-- Related to the connection properties -->

<property name="myeclipse.connection.profile">myJdbcDriver</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">lms</property>
<property name="connection.password">scott</property>


<!-- Related to the hibernate properties -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<!-- Related to hibernate mapping -->
<mapping resource="com/hiber/Student.hbm.xml" />
<mapping resource="com/hiber/Course.hbm.xml" />

</session-factory>

</hibernate-configuration>
Client Application 1 :
ClientApp.java
package com.client;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=new Student();
student.setStudentId(101);
student.setStuentName("Ashok");

Course course=new Course();
course.setCourseId(201);
course.setCourseName("hibernate");

Course course2=new Course();
course2.setCourseId(202);
course2.setCourseName("spring");

Set set=new HashSet();
set.add(course);
set.add(course2);

student.setCourses(set);

session.save(student);
tx.commit();

System.out.println("success");
}

}
Client Application 2 :
ClientApp.java
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=new Student();
student.setStudentId(101);
student.setStuentName("Arun");

Course course=new Course();
course.setCourseId(201);
course.setCourseName("hibernate");
course.setStudent(student);

Course course2=new Course();
course2.setCourseId(202);
course2.setCourseName("spring");
course2.setStudent(student);

session.save(course);
session.save(course2);

tx.commit();
System.out.println("success");
}

}
Client Application 3 :
ClientApp.java
package com.client;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){
	


Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=new Student();
student.setStudentId(101);
student.setStuentName("Akshay");

Course course=new Course();
course.setCourseId(201);
course.setCourseName("hibernate");
course.setStudent(student);

Course course2=new Course();
course2.setCourseId(202);
course2.setCourseName("spring");
course2.setStudent(student); 

Set set=new HashSet();
set.add(course);
set.add(course2);


session.save(course2);

tx.commit();
System.out.println("success");

}
}

Many to Many mapping :

  1. Many to many is nothing but applying one to many relationship on both sides.
    Ex: one student can join in multiple courses and one course can contain multiple students.
  2. Here we need collection property in both pojo classes and we need to configure this collection details in both mapping files.
  3. While configuring collection property details we need to use <many-to-one> to map the relationship.

Note : In many to many relationship a mediator table is mandatory. We can call this table as join table. This join table contains only foreign keys.

Example :

Student.java
package com.beans;

import java.util.Set;

public class Student {
private int studentId;
private String stuentName;

private Set courses;

public int getStudentId() {
	return studentId;
}
public void setStudentId(int studentId) {
	this.studentId = studentId;
}
public String getStuentName() {
	return stuentName;
}
public void setStuentName(String stuentName) {
	this.stuentName = stuentName;
}
public Set getCourses() {
	return courses;
}
public void setCourses(Set courses) {
	this.courses = courses;
}

}

Course.java
package com.beans;

import java.util.Set;

public class Course {
private int courseId;
private String courseName;

private Set students;

public int getCourseId() {
	return courseId;
}
public void setCourseId(int courseId) {
	this.courseId = courseId;
}
public String getCourseName() {
	return courseName;
}
public void setCourseName(String courseName) {
	this.courseName = courseName;
}
public Set getStudents() {
	return students;
}
public void setStudents(Set students) {
	this.students = students;
}

}

Student.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Student" table="STUDENTDB">
  <id name="studentId" column="SID"/>
  <property name="stuentName" column="SNAME" length="15"/> 
  
  <set name="courses" cascade="all" table="STUDENTCOURSE">
   <key column="refSID"/>
   <many-to-many class="com.beans.Course" column="refCID"/>
  </set>
        
</class>

</hibernate-mapping>
Course.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Course" table="COURSEDB">
  <id name="courseId" column="CID"/>
  <property name="courseName" column="CNAME" length="15"/> 
  
  <set name="students" cascade="all" table="STUDENTCOURSE">
   <key column="refCID"/>
   <many-to-many class="com.beans.Student" column="refSID"/>
  </set>    
</class>

</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>

<session-factory>

<!-- Related to the connection properties -->

<property name="myeclipse.connection.profile">myJdbcDriver</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">lms</property>
<property name="connection.password">scott</property>


<!-- Related to the hibernate properties -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<!-- Related to hibernate mapping -->
<mapping resource="com/hiber/Student.hbm.xml" />
<mapping resource="com/hiber/Course.hbm.xml" />

</session-factory>

</hibernate-configuration>
Client Application 1 :
ClientApp.java
package com.client;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){
	


Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=new Student();
student.setStudentId(101);
student.setStuentName("Akshay");

Student student2=new Student();
student2.setStudentId(102);
student2.setStuentName("Arun");

Course course=new Course();
course.setCourseId(201);
course.setCourseName("hibernate");

Course course2=new Course();
course2.setCourseId(202);
course2.setCourseName("spring");


Set set=new HashSet();
set.add(course);
set.add(course2);

student.setCourses(set);
student2.setCourses(set);

session.save(student);
session.save(student2);

tx.commit();
System.out.println("success");

}
}

One to One mapping :

  1. In one to one mapping, one pojo class object is related with one pojo class object only.
  2. Here foreign key is not required, we need to copy the primary key value of parent object into primary key value of child object.
  3. To achieve this relationship we need to use foreign generator.

Example :

Student.java
package com.beans;

import java.util.Set;

public class Student {
private int studentId;
private String stuentName;

private Course course;

public int getStudentId() {
	return studentId;
}
public void setStudentId(int studentId) {
	this.studentId = studentId;
}
public String getStuentName() {
	return stuentName;
}
public void setStuentName(String stuentName) {
	this.stuentName = stuentName;
}
public Course getCourse() {
	return course;
}
public void setCourse(Course course) {
	this.course = course;
}

}
Course.java
package com.beans;

public class Course {
private int courseId;
private String courseName;

public int getCourseId() {
	return courseId;
}
public void setCourseId(int courseId) {
	this.courseId = courseId;
}
public String getCourseName() {
	return courseName;
}
public void setCourseName(String courseName) {
	this.courseName = courseName;
}

}
Student.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Student" table="STUDENTDB">
  <id name="studentId" column="SID">
   <generator class="foreign">
     <param name="property">course</param>
   </generator>
  </id>
  
  <property name="stuentName" column="SNAME" length="15"/> 
    
  <one-to-one name="course" class="com.beans.Course" cascade="all"/> 
        
</class>

</hibernate-mapping>

Course.hbm.xml
<!-- Related to hibernate mapping for POJO -->
<hibernate-mapping>

<class name="com.beans.Course" table="COURSEDB">
  <id name="courseId" column="CID"/>
  <property name="courseName" column="CNAME" length="15"/> 
       
</class>

</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>

<session-factory>

<!-- Related to the connection properties -->

<property name="myeclipse.connection.profile">myJdbcDriver</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">lms</property>
<property name="connection.password">scott</property>


<!-- Related to the hibernate properties -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>

<!-- Related to hibernate mapping -->
<mapping resource="com/hiber/Student.hbm.xml" />
<mapping resource="com/hiber/Course.hbm.xml" />

</session-factory>

</hibernate-configuration>
Client Application 1 :
ClientApp.java
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Course;
import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=new Student();
student.setStudentId(101);
student.setStuentName("Akshay");

Course course=new Course();
course.setCourseId(201);
course.setCourseName("Ashok");

student.setCourse(course);

session.save(student);

tx.commit();
System.out.println("success");

}
}
Client Application 2 :
ClientApp.java
package com.client;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.beans.Student;

public class ClientApp {

public static void main(String ar[]){

Configuration cfg=new Configuration();
cfg.configure();

SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();

Student student=(Student)session.get(Student.class, 101);
session.delete(student);

tx.commit();
System.out.println("success");

}
}