0% found this document useful (0 votes)
90 views

Hibernate Labs Example

This document contains code examples demonstrating the use of Hibernate for object relational mapping. It shows how to: 1. Define entity classes like Student with collections mapped to tables in a database. 2. Configure Hibernate and establish sessions to save, update, delete and load objects. 3. Save a Student object to the database with collections like emails, marks, courses mapped between the Student table and other tables. 4. Load the Student object back from the database to retrieve and display the collections.

Uploaded by

Mohammed Jeelan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Hibernate Labs Example

This document contains code examples demonstrating the use of Hibernate for object relational mapping. It shows how to: 1. Define entity classes like Student with collections mapped to tables in a database. 2. Configure Hibernate and establish sessions to save, update, delete and load objects. 3. Save a Student object to the database with collections like emails, marks, courses mapped between the Student table and other tables. 4. Load the Student object back from the database to retrieve and display the collections.

Uploaded by

Mohammed Jeelan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

TLab1:Core exmaple

1)Table Employee is required


create database dbtest;
use dbtest;
create table Employee(
eid int primary key,
ename char(10),
email char(10),
phone long,
company char(10),
salary double,
status char(10)
);
2)Client code
-----------------------package com.jlcindia.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class Client {
public static void main(String[] args) {
try{
employee emp=new
employee("raju","[email protected]",1111,"sdsoft",2222.0,"enable");
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Integer it=(Integer)session.save(emp);
System.out.println(it.intValue());
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}}}
3)employee.java
package com.jlcindia.hibernate;
public class employee {
private int eid;
private String ename;
private String email;
private long phone;
private String company;

private double salary;


private String status;
public employee(){}
public employee( String ename, String email, long phone,
String company,double salary,String status) {
super();
this.ename = ename;
this.email = email;
this.phone = phone;
this.company = company;
this.salary = salary;
this.status = status;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;

}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}}
4)Employee.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 package="com.jlcindia.hibernate">
<class name="employee" table="Employee">
<id name="eid" column="eid" type="int">
<generator class="increment" />
</id>
<property name="ename"/>
<property name="email" type="string"/>
<property name="phone" column="phone" type="long"/>
<property name="company" column="company" type="string"/>
<property name="salary" column="salary" type="double"/>
<property name="status" column="status" type="string"/>
</class>
</hibernate-mapping>
5)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-configuration3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop
erty>
<property
name="hibernate.connection.url">jdbc:mysql://localhost/dbtest</proper
ty>
<property name="hibernate.connection.username">root</property>
<property
name="hibernate.connection.password">jlcindia</property>
<property
name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/jlcindia/hibernate/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TLab1:Annotation example
Client.java
package com.jlcindia.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
public class Client {
public static void main(String[] args){
try{
employee emp=new
employee(13,"raju","[email protected]",1111,"sdsoft",2222.0,"enable");
AnnotationConfiguration cfg=new AnnotationConfiguration();
cfg=(AnnotationConfiguration)cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Integer it=(Integer)session.save(emp);
System.out.println(it.intValue());
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}}}

Employee.java
package com.jlcindia.hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="customers")
public class employee {
@Column(name="eid")
@Id
private int eid;

@Column(name="ename")
private String ename;
@Column(name="email")
private String email;
@Column(name="phone")
private long phone;
@Column(name="company")
private String company;
@Column(name="salary")
private double salary;
@Column(name="status")
private String status;
public employee(){}
public employee(int eid, String ename, String email, long phone,
String company, double salary, String status) {
super();
this.eid = eid;
this.ename = ename;
this.email = email;
this.phone = phone;
this.company = company;
this.salary = salary;
this.status = status;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public long getPhone() {
return phone;
}
public void setPhone(long phone) {
this.phone = phone;
}

public String getCompany() {


return company;
}
public void setCompany(String company) {
this.company = company;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}}
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-configuration3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop
erty>
<property
name="hibernate.connection.url">jdbc:mysql://localhost/dbtest</proper
ty>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">jlcindia</property>
<property
name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.jlcindia.hibernate.employee"/>
</session-factory>
</hibernate-configuration>

CLab2:Collection Mapping:
1)HibernateUtil.java
package com.jlcindia.util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
static SessionFactory sessionFactory=null;
static{
Configuration cfg=new Configuration();
cfg=cfg.configure();
sessionFactory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}}
2)HibernateTemplate.java
package com.jlcindia.util;
import java.io.*;
import org.hibernate.*;
import com.jlcindia.hibernate.*;
public class HibernateTemplate {
public static Object saveObject(Object obj){
Object id=null;
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
id=session.save(obj);
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
return id;
}
public static void updateObject(Object obj){
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
session.update(obj);
tx.commit();
session.close();

}catch(Exception e){
e.printStackTrace();
}
}
public static void deleteObject(Class cls,Serializable s){
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Object o=session.load(cls,s);
session.delete(o);
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static Object loadObject(Class cls,Serializable s){
Object o=null;
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
o=session.load(cls,s);
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
return o;
}
}
Client.java
package com.jlcindia.hibernate;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.jlcindia.util.HibernateTemplate;
public class Client {

public static void main(String[] args){


try{
//1. adding the Student
List<String>emails=new ArrayList<String>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
List<Integer>marks=new ArrayList<Integer>();
marks.add(10);
marks.add(20);
marks.add(30);
marks.add(40);
String[] courses={"Java","JSP","JDBC","EJB"};
Set<Long> phones=new HashSet<Long>();
phones.add(new Long(1111));
phones.add(new Long(2222));
phones.add(new Long(3333));
phones.add(new Long(4444));
Map<String,Long>refs=new HashMap<String,Long>();
refs.put("AA", new Long(11));
refs.put("AA", new Long(22));
refs.put("AA", new Long(33));
refs.put("AA", new Long(44));
Student stu=new
Student("sri","sri","Blore","Enabled",emails,marks,courses,phones,refs);
Integer it=(Integer)HibernateTemplate.saveObject(stu);
System.out.println(it.intValue());
//2.loading the Student
stu=(Student)HibernateTemplate.loadObject(Student.class, 1);
System.out.println(stu.getSid()+"\t"+stu.getFname()
+"\t"+stu.getLname()+"\t"+stu.getCity()+"\t"+stu.getStatus());
System.out.println(stu.getEmails());
System.out.println(stu.getMarks());
System.out.println(stu.getCourses());
System.out.println(stu.getPhones());
System.out.println(stu.getRefs());
}catch(Exception e){
e.printStackTrace();
}}}

Student.java

package com.jlcindia.hibernate;
import java.util.*;
public class Student {
private int sid;
private String fname;
private String lname;
private String city;
private String status;
private List<String>emails;
private List<Integer>marks;
private String[] courses;
private Set<Long>phones;
private Map<String,Long>refs;
public Student(){}
public Student(String fname, String lname, String city, String status,
List<String> emails, List<Integer> marks, String[]
courses, Set<Long> phones, Map<String, Long> refs) {
super();
this.fname = fname;
this.lname = lname;
this.city = city;
this.status = status;
this.emails = emails;
this.marks = marks;
this.courses = courses;
this.phones = phones;
this.refs = refs;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;

}
public void setStatus(String status) {
this.status = status;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
public List<Integer> getMarks() {
return marks;
}
public void setMarks(List<Integer> marks) {
this.marks = marks;
}
public String[] getCourses() {
return courses;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public Set<Long> getPhones() {
return phones;
}
public void setPhones(Set<Long> phones) {
this.phones = phones;
}
public Map<String, Long> getRefs() {
return refs;
}
public void setRefs(Map<String, Long> refs) {
this.refs = refs;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}}
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 package="com.jlcindia.hibernate"
default-lazy="false">
<class name="Student" table="students" lazy="false">
<id name="sid" column="sid" type="int">
<generator class="increment" />
</id>
<property name="fname" />
<property name="lname" />
<property name="city" />
<property name="status" />
<list name="emails" table="emails">
<key column="sid" />
<index column="idx" />
<element column="emailId" type="string" />
</list>
<bag name="marks" table="marks">
<key column="sid" />
<element column="marks" type="int" />
</bag>
<array name="courses" table="courses">
<key column="sid" />
<index column="idx" />
<element column="course" type="string" />
</array>
<set name="phones" table="phones">
<key column="sid" />
<element column="phoneNo" type="long" />
</set>
<map name="refs" table="refs">
<key column="sid" />
<index column="rname" type="string" />
<element column="rphone" type="long" />
</map>
</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-configuration3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop
erty>
<property
name="hibernate.connection.url">jdbc:mysql://localhost/jlc1db</proper
ty>
<property name="hibernate.connection.username">root</property>
<property
name="hibernate.connection.password">srinivas</property>
<property
name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/jlcindia/hibernate/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
ALab2: Collection Mapping:
HibernateUtil.java
package com.jlcindia.util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
static SessionFactory sessionFactory=null;
static{
AnnotationConfiguration cfg=new AnnotationConfiguration();
cfg=(AnnotationConfiguration)cfg.configure();
sessionFactory=cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
HibernateTemplate.java
package com.jlcindia.util;

import java.io.*;
import org.hibernate.*;
import com.jlcindia.hibernate.*;
public class HibernateTemplate {
public static Object saveObject(Object obj){
Object id=null;
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
id=session.save(obj);
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
return id;
}
public static void updateObject(Object obj){
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
session.update(obj);
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void deleteObject(Class cls,Serializable s){
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Object o=session.load(cls,s);
session.delete(o);
tx.commit();
session.close();

}catch(Exception e){
e.printStackTrace();
}
}
public static Object loadObject(Class cls,Serializable s){
Object o=null;
try{
SessionFactory sf=HibernateUtil.getSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
o=session.load(cls,s);
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
return o;
}}
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-configuration3.0.dtd">
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop
erty>
<property
name="hibernate.connection.url">jdbc:mysql://localhost/jlc2db</proper
ty>
<property name="hibernate.connection.username">root</property>
<property
name="hibernate.connection.password">srinivas</property>
<property
name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.jlcindia.hibernate.Student"/>
</session-factory>
</hibernate-configuration>

Client.java
package com.jlcindia.hibernate;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
import com.jlcindia.util.HibernateTemplate;
public class Client {
public static void main(String[] args){
try{
//1. adding the Student
List<String>emails=new ArrayList<String>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
List<Integer>marks=new ArrayList<Integer>();
marks.add(10);
marks.add(20);
marks.add(30);
marks.add(40);
String[] courses={"Java","JSP","JDBC","EJB"};
Set<Long> phones=new HashSet<Long>();
phones.add(new Long(1111));
phones.add(new Long(2222));
phones.add(new Long(3333));
phones.add(new Long(4444));
Map<String,Long>refs=new HashMap<String,Long>();
refs.put("AA", new Long(11));
refs.put("AA", new Long(22));
refs.put("AA", new Long(33));
refs.put("AA", new Long(44));
Student stu=new
Student(8,"sri","sri","Blore","Enabled",emails,marks,courses,phones,refs)
;
Integer it=(Integer)HibernateTemplate.saveObject(stu);
System.out.println(it.intValue());
//2.loading the Student
stu=(Student)HibernateTemplate.loadObject(Student.class,3);
System.out.println(stu.getSid()+"\t"+stu.getFname()
+"\t"+stu.getLname()+"\t"+stu.getCity()+"\t"+stu.getStatus());
System.out.println(stu.getEmails());

System.out.println(stu.getMarks());
System.out.println(stu.getCourses());
System.out.println(stu.getPhones());
System.out.println(stu.getRefs());
}catch(Exception e){
e.printStackTrace();
}}}
Student.java
package com.jlcindia.hibernate;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.*;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.Proxy;
@Entity
@Table(name="students")
@Proxy(lazy=false)
public class Student {
@Id
@Column(name="sid")
private int sid;
@Column(name="fname")
private String fname;
@Column(name="lname")
private String lname;
@Column(name="city")
private String city;
@Column(name="status")
private String status;
@CollectionOfElements
@JoinTable(name="emails",
joinColumns=@JoinColumn(name="sid"))
@IndexColumn(name="idx")
@Column(name="emailId")
private List<String>emails;
@CollectionOfElements
@JoinTable(name="marks",
joinColumns=@JoinColumn(name="sid"))
@IndexColumn(name="idx")
@Column(name="marks")
private List<Integer>marks;
@CollectionOfElements
@JoinTable(name="courses",
joinColumns=@JoinColumn(name="sid"))

@IndexColumn(name="idx")
@Column(name="course")
private String[] courses;
@CollectionOfElements
@JoinTable(name="phones",
joinColumns=@JoinColumn(name="sid"))
@Column(name="phoneNo")
private Set<Long>phones;
@CollectionOfElements
@JoinTable(name="refs",
joinColumns=@JoinColumn(name="sid"))
@IndexColumn(name="idx")
@Column(name="rphone")
private Map<String,Long>refs;
public Student(){}
public Student(int sid, String fname, String lname, String city,
String status, List<String> emails, List<Integer> marks,
String[] courses, Set<Long> phones, Map<String, Long>
refs) {
super();
this.sid = sid;
this.fname = fname;
this.lname = lname;
this.city = city;
this.status = status;
this.emails = emails;
this.marks = marks;
this.courses = courses;
this.phones = phones;
this.refs = refs;
}
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 getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;

}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
public List<Integer> getMarks() {
return marks;
}
public void setMarks(List<Integer> marks) {
this.marks = marks;
}
public String[] getCourses() {
return courses;
}
public void setCourses(String[] courses) {
this.courses = courses;
}
public Set<Long> getPhones() {
return phones;
}
public void setPhones(Set<Long> phones) {
this.phones = phones;
}
public Map<String, Long> getRefs() {
return refs;
}
public void setRefs(Map<String, Long> refs) {
this.refs = refs;
}}

Inheritance Mapping Example

A)Table per subclass mapping


CLab3:Client.java
package com.jlcindia.hibernate;
import java.util.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Client {
public static void main(String[] args){
try{
//1. adding the Student
List<String>emails=new ArrayList<String>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
Set<Long>phones=new HashSet<Long>();
phones.add(new Long(1111));
phones.add(new Long(2222));
phones.add(new Long(3333));
phones.add(new Long(4444));
Student stu=new
Student("sri","Blore","Enabled",15000.0,emails,phones);
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Integer it=(Integer)session.save(stu);
System.out.println(it.intValue());
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
try{
//1. adding the CurrentStudent
List<String>emails=new ArrayList<String>();

emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
Set<Long>phones=new HashSet<Long>();
phones.add(new Long(1111));
phones.add(new Long(2222));
phones.add(new Long(3333));
phones.add(new Long(4444));
CurrentStudent cstu=new
CurrentStudent("sri","Blore","Enabled",15000.0,emails,phones,2000.0,"6.
30PM","Mathikere");
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Integer it=(Integer)session.save(cstu);
System.out.println(it.intValue());
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
try{
//1. adding the OldStudent
List<String>emails=new ArrayList<String>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
Set<Long>phones=new HashSet<Long>();
phones.add(new Long(1111));
phones.add(new Long(2222));
phones.add(new Long(3333));
phones.add(new Long(4444));
OldStudent ostu=new OldStudent
("sri","Blore","Enabled",15000.0,emails,phones,"SDSOFT","[email protected]",9.
0);
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();

Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Integer it=(Integer)session.save(ostu);
System.out.println(it.intValue());
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
try{
//1. adding the RegularStudent
List<String>emails=new ArrayList<String>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");
Set<Long>phones=new HashSet<Long>();
phones.add(new Long(1111));
phones.add(new Long(2222));
phones.add(new Long(3333));
phones.add(new Long(4444));
RegularStudent ostu=new RegularStudent
("sri","Blore","Enabled",15000.0,emails,phones,2000.0,"6.30P.M","Mathik
ere","M.sc","85.5",3);
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Integer it=(Integer)session.save(ostu);
System.out.println(it.intValue());
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
try{
//1. adding the WeekendStudent
List<String>emails=new ArrayList<String>();
emails.add("[email protected]");
emails.add("[email protected]");
emails.add("[email protected]");

emails.add("[email protected]");
Set<Long>phones=new HashSet<Long>();
phones.add(new Long(1111));
phones.add(new Long(2222));
phones.add(new Long(3333));
phones.add(new Long(4444));
WeekendStudent wstu=new
WeekendStudent("sri","Blore","Enabled",15000.0,emails,phones,2000.0,"
6.30P.M","HSR","SDSOFT","[email protected]",9.0);
Configuration cfg=new Configuration();
cfg=cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Integer it=(Integer)session.save(wstu);
System.out.println(it.intValue());
tx.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}}}
Hibernate.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 package="com.jlcindia.hibernate"
default-lazy="false">
<class name="Student" table="students1" lazy="false">
<id name="sid" column="sid" type="int">
<generator class="increment" />
</id>
<property name="sname" />
<property name="city" />
<property name="status" />
<property name="totalfee" type="double" />
<list name="emails" table="emails1">
<key column="sid" />
<index column="idx" />
<element column="emailId" type="string" />
</list>
<set name="phones" table="phones1">

<key column="sid" />


<element column="phoneNo" type="long" />
</set>
<joined-subclass name="CurrentStudent"
table="cstudents1">
<key column="sid" />
<property name="feebal" type="double" />
<property name="timings" />
<property name="branch" />
<joined-subclass name="RegularStudent"
table="rstudents1">
<key column="sid" />
<property name="qualification" />
<property name="percentage" />
<property name="yoe" type="int" />
</joined-subclass>
<joined-subclass name="WeekendStudent"
table="wstudents1">
<key column="sid" />
<property name="company" />
<property name="cemail" />
<property name="ctc" type="double" />
</joined-subclass>
</joined-subclass>
<joined-subclass name="OldStudent" table="ostudents1">
<key column="sid" />
<property name="company" />
<property name="cemail" />
<property name="ctc" type="double" />
</joined-subclass>
</class>
</hibernate-mapping>
Student.java
package com.jlcindia.hibernate;
import java.util.*;
public class Student {
private int sid;
private String sname;
private String city;
private String status;
private double totalfee;
private List<String>emails;

private Set<Long>phones;
public Student(){}
public Student(String sname, String city, String status, double
totalfee,
List<String> emails, Set<Long> phones) {
super();
this.sname = sname;
this.city = city;
this.status = status;
this.totalfee = totalfee;
this.emails = emails;
this.phones = phones;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}

public double getTotalfee() {


return totalfee;
}
public void setTotalfee(double totalfee) {
this.totalfee = totalfee;
}
public List<String> getEmails() {
return emails;
}
public void setEmails(List<String> emails) {
this.emails = emails;
}
public Set<Long> getPhones() {
return phones;
}
public void setPhones(Set<Long> phones) {
this.phones = phones;
}
}
CurrentStudent.java
package com.jlcindia.hibernate;
import java.util.List;
import java.util.Set;
public class CurrentStudent extends Student {
private double feebal;
private String timings;
private String branch;
public CurrentStudent(String sname, String city, String status,
double totalfee, List<String> emails, Set<Long> phones,
double feebal, String timings, String branch) {
super(sname, city, status, totalfee, emails, phones);
this.feebal = feebal;
this.timings = timings;
this.branch = branch;
}
public double getFeebal() {
return feebal;

}
public void setFeebal(double feebal) {
this.feebal = feebal;
}
public String getTimings() {
return timings;
}
public void setTimings(String timings) {
this.timings = timings;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
this.branch = branch;
}
}
OldStudent.java
package com.jlcindia.hibernate;
import java.util.List;
import java.util.Set;
public class OldStudent extends Student {
private String company;
private String cemail;
private double ctc;
public OldStudent(String sname, String city, String status,
double totalfee, List<String> emails, Set<Long> phones,
String company, String cemail, double ctc) {
super(sname, city, status, totalfee, emails, phones);
this.company = company;
this.cemail = cemail;
this.ctc = ctc;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCemail() {
return cemail;
}
public void setCemail(String cemail) {
this.cemail = cemail;
}

public double getCtc() {


return ctc;
}
public void setCtc(double ctc) {
this.ctc = ctc;
}}
RegularStudent.java
package com.jlcindia.hibernate;
import java.util.List;
import java.util.Set; package com.jlcindia.hibernate;
import java.util.List;
import java.util.Set;
public class WeekendStudent extends CurrentStudent {
private String company;
private String cemail;
private double ctc;
public WeekendStudent(String sname, String city, String status,
double totalfee, List<String> emails, Set<Long> phones,
double feebal, String timings, String branch, String company, String
cemail, double ctc) {
super(sname, city, status, totalfee, emails, phones, feebal, timings,
branch);
this.company = company;
this.cemail = cemail;
this.ctc = ctc;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCemail() {
return cemail;
}
public void setCemail(String cemail) {
this.cemail = cemail;
}
public double getCtc() {
return ctc;
}
public void setCtc(double ctc) {
this.ctc = ctc;
}

}
WeekendStudent.java
package com.jlcindia.hibernate;
import java.util.List;
import java.util.Set;
public class WeekendStudent extends CurrentStudent {
private String company;
private String cemail;
private double ctc;
public WeekendStudent(String sname, String city, String status,
double totalfee, List<String> emails, Set<Long> phones,
double feebal, String timings, String branch, String
company, String cemail, double ctc) {
super(sname, city, status, totalfee, emails, phones, feebal, timings,
branch);
this.company = company;
this.cemail = cemail;
this.ctc = ctc;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getCemail() {
return cemail;
}
public void setCemail(String cemail) {
this.cemail = cemail;
}
public double getCtc() {
return ctc;
}
public void setCtc(double ctc) {
this.ctc = ctc;
}}

You might also like