Saturday, April 21, 2012

Hibernate Two Classes one Table


package com.hibernate.venkat;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;



/**
 * @author Venkat
 *
 */
@Entity
public class School {

/**
* @param args
*/
private int schoolId;
private String schoolName;
private SchoolDetail schoolDetail;


@Id
@GeneratedValue
public int getSchoolId() {
return schoolId;
}
public void setSchoolId(int schoolId) {
this.schoolId = schoolId;
}
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
@Embedded
public SchoolDetail getSchoolDetail() {
return schoolDetail;
}
public void setSchoolDetail(SchoolDetail schoolDetail) {
this.schoolDetail = schoolDetail;
}

}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;

import javax.persistence.Embeddable;

@Embeddable
public class SchoolDetail {

private String schoolAddress;
private boolean isPublicSchool;
private int studentCount;
public String getSchoolAddress() {
return schoolAddress;
}
public void setSchoolAddress(String schoolAddress) {
this.schoolAddress = schoolAddress;
}
public boolean isPublicSchool() {
return isPublicSchool;
}
public void setPublicSchool(boolean isPublicSchool) {
this.isPublicSchool = isPublicSchool;
}
public int getStudentCount() {
return studentCount;
}
public void setStudentCount(int studentCount) {
this.studentCount = studentCount;
}
}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestSchool {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(School.class);
config.configure("hibernate.cfg.xml");

new SchemaExport(config).create(true, true);

SessionFactory sf=config.buildSessionFactory();
Session s= sf.getCurrentSession();
s.beginTransaction();

SchoolDetail sd=new SchoolDetail();
sd.setPublicSchool(true);
sd.setSchoolAddress("Atlanta GA");
sd.setStudentCount(230);

School s1= new School();
s1.setSchoolName("Venkat School");
s1.setSchoolDetail(sd);

s.save(s1);
s.getTransaction().commit();

}

}
---------------------------------------------------------------------------------------------
<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

Hibernate One to One Mapping Bidirectional


package com.hibernate.venkat;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Person {


private int personId;
private String personName;

private PersonDetail pdetail;

@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="pdetal_FK")
public PersonDetail getPdetail() {
return pdetail;
}
public void setPdetail(PersonDetail pdetail) {
this.pdetail = pdetail;
}
@Id
@GeneratedValue
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}



}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class PersonDetail {

private int personDetailId;
private String zipcode;
private String job;
private double income;
private Person person;
@OneToOne(mappedBy="pdetail", cascade=CascadeType.ALL)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
@Id
@GeneratedValue
@Column(name="detail_pk")
public int getPersonDetailId() {
return personDetailId;
}
public void setPersonDetailId(int personDetailId) {
this.personDetailId = personDetailId;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
}
---------------------------------------------------------------------------------------------
/**
 * 
 */
package com.hibernate.venkat;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestPerson {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Person.class);
config.addAnnotatedClass(PersonDetail.class);
config.configure();
//new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
PersonDetail pd=new PersonDetail();
pd.setIncome(99756.89);
pd.setJob("Developer");
pd.setZipcode("30328");
Person venkat=new Person();
venkat.setPersonName("Venkat");
venkat.setPdetail(pd);
s.save(venkat);
s.getTransaction().commit();

}

}
-------------------------------------------------------------------------------------------------
<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

Hibernate One to One Mapping Example


package com.hibernate.venkat;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Person {


private int personId;
private String personName;

private PersonDetail pdetail;

@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="pdetal_FK")
public PersonDetail getPdetail() {
return pdetail;
}
public void setPdetail(PersonDetail pdetail) {
this.pdetail = pdetail;
}
@Id
@GeneratedValue
public int getPersonId() {
return personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}



}
---------------------------------------------------------------------------------------------

package com.hibernate.venkat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class PersonDetail {

private int personDetailId;
private String zipcode;
private String job;
private double income;
@Id
@GeneratedValue
@Column(name="detail_pk")
public int getPersonDetailId() {
return personDetailId;
}
public void setPersonDetailId(int personDetailId) {
this.personDetailId = personDetailId;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getIncome() {
return income;
}
public void setIncome(double income) {
this.income = income;
}
}
---------------------------------------------------------------------------------------------

/**
 * 
 */
package com.hibernate.venkat;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestPerson {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Person.class);
config.addAnnotatedClass(PersonDetail.class);
config.configure();
new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
PersonDetail pd=new PersonDetail();
pd.setIncome(99756.89);
pd.setJob("Developer");
pd.setZipcode("30328");
Person venkat=new Person();
venkat.setPersonName("Venkat");
venkat.setPdetail(pd);
s.save(venkat);
//s.save(pd); no need if we set cascadetype
s.getTransaction().commit();

}

}
---------------------------------------------------------------------------------------------
<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

Hibernate One to Many Mapping Example


package com.hibernate.venkat;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class College {


private int collegeId;
private String collegeName;
private List<Student> students;

@Id
@GeneratedValue
public int getCollegeId() {
return collegeId;
}
public void setCollegeId(int collegeId) {
this.collegeId = collegeId;
}
public String getCollegeName() {
return collegeName;
}
public void setCollegeName(String collegeName) {
this.collegeName = collegeName;
}
@OneToMany(targetEntity=Student.class,mappedBy="college",cascade=CascadeType.ALL,fetch=FetchType.LAZY)
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "College [collegeId=" + collegeId + ", collegeName="
+ collegeName + ", students=" + students + "]";
}



}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Student {

private int studentId;
private String studentName;
private College  college;
@Id
@GeneratedValue
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@ManyToOne
@JoinColumn(name="College_id")
public College getCollege() {
return college;
}
public void setCollege(College college) {
this.college = college;
}
}
----------------------------------------------------------------------------------
/**
 * 
 */
package com.hibernate.venkat;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestOneToMany {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(College.class);
config.addAnnotatedClass(Student.class);
config.configure();
new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
College c=new College();
c.setCollegeName("Georgia TECH");
Student s1=new Student();
s1.setStudentName("Venkat");
s1.setCollege(c);
Student s2=new Student();
s2.setStudentName("Sunil");
s2.setCollege(c);
s.save(c);
s.save(s1);
s.save(s2);
s.getTransaction().commit();
}

}
-------------------------------------------------------------------------
<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

Hibernate One Class Two Tables


package com.hibernate.venkat;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;

@Entity
@Table(name="CustomeInfo")
@SecondaryTable(name="CustomerDetails")
public class Customer {


private int custId;
private String custName;
private String custAddress;
private int creditScore;
private int rewardPoints;

@Id
@GeneratedValue
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
@Column(table="CustomerDetails")
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
@Column(table="CustomerDetails")
public int getCreditScore() {
return creditScore;
}
public void setCreditScore(int creditScore) {
this.creditScore = creditScore;
}
@Column(table="CustomerDetails")
public int getRewardPoints() {
return rewardPoints;
}
public void setRewardPoints(int rewardPoints) {
this.rewardPoints = rewardPoints;
}


}
---------------------------------------------------------------------------------------------

/**
 * 
 */
package com.hibernate.venkat;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestCustomer {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Customer.class);
config.configure();
new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
Customer venkat=new Customer();
venkat.setCreditScore(888);
venkat.setCustId(100);
venkat.setCustAddress("Atlanta");
venkat.setCustName("Venkat");
venkat.setRewardPoints(23444);
s.save(venkat);
s.getTransaction().commit();

}

}
---------------------------------------------------------------------------------------------

<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>

Hibernate Many to Many Example


<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>
---------------------------------------------------------------------------------------------

package com.hibernate.venkat;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

@Entity
public class Event {


private int eventId;
private String eventName;
private List<Delegate> delegate=new ArrayList<Delegate>();

@Id
@GeneratedValue
public int getEventId() {
return eventId;
}
public void setEventId(int eventId) {
this.eventId = eventId;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
@ManyToMany
@JoinTable(name="Join_Delegate_Event",
joinColumns={@JoinColumn(name="eventId")},
inverseJoinColumns={@JoinColumn(name="delegateId")})
public List<Delegate> getDelegate() {
return delegate;
}
public void setDelegate(List<Delegate> delegate) {
this.delegate = delegate;
}



}
---------------------------------------------------------------------------------------------
/**
 * 
 */
package com.hibernate.venkat;

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

import javax.persistence.EntityManager;
import javax.persistence.JoinTable;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestEvent {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Event.class);
config.addAnnotatedClass(Delegate.class);
config.configure();
// new SchemaExport(config).create(true, true);
SessionFactory sf= config.buildSessionFactory();
Session s=sf.getCurrentSession();
s.beginTransaction();
// Delegate d1=new Delegate();
// d1.setDelegateName("Venkat");
// Delegate d2=new Delegate();
// d2.setDelegateName("Sunil");
// Delegate d3=new Delegate();
// d3.setDelegateName("Naresh");
// Delegate d4=new Delegate();
// d4.setDelegateName("Pavan");
// Delegate d5=new Delegate();
// d5.setDelegateName("Satya");
//
// Event e1=new Event();
// e1.setEventName("JAVA");
// Event e2=new Event();
// e2.setEventName("Hibernate");
// Event e3=new Event();
// e3.setEventName("Spring");
// Event e4=new Event();
// e4.setEventName("Flex");
// Event e5=new Event();
// e5.setEventName("Webservices");
//
// e1.getDelegate().add(d1);
// e1.getDelegate().add(d2);
//
// e2.getDelegate().add(d2);
// e1.getDelegate().add(d3);
// e3.getDelegate().add(d4);
// e2.getDelegate().add(d2);
// e2.getDelegate().add(d5);
// e4.getDelegate().add(d1);
//
// s.save(d1);
// s.save(d2);
// s.save(d3);s.save(d4);
// s.save(d5);
//
// s.save(e1);
// s.save(e2);
// s.save(e3);
// s.save(e4);
// s.save(e5);
//
Query q=s.createQuery("from Event");
List l=q.list();
for (Iterator iterator = l.iterator(); iterator.hasNext();) {
Event object = (Event) iterator.next();
System.out.println(object.getEventName());
}

}
}
---------------------------------------------------------------------------------------------

<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>


Hibernate Inheritance


/**
 *
 */
package com.hibernate.venkat;

import javax.persistence.Entity;

/**
 * @author Venkat
 *
 */
@Entity
public class Module extends Project {

private String ModuleName;

public String getModuleName() {
return ModuleName;
}

public void setModuleName(String moduleName) {
ModuleName = moduleName;
}
}
---------------------------------------------------------------------------------------------
package com.hibernate.venkat;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;

/**
 * @author Venkat
 *
 */
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class Project {

private int projectId;
private String projectName;
@Id
@GeneratedValue
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
}
---------------------------------------------------------------------------------------------
/**
 * 
 */
package com.hibernate.venkat;

import javax.persistence.Entity;

/**
 * @author Venkat
 *
 */
@Entity
public class Task extends Module{
private String taskName;

public String getTaskName() {
return taskName;
}

public void setTaskName(String taskName) {
this.taskName = taskName;
}

}
---------------------------------------------------------------------------------------------
/**
 * 
 */
package com.hibernate.venkat;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @author Venkat
 *
 */
public class TestInheritance {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
AnnotationConfiguration config=new AnnotationConfiguration();
config.addAnnotatedClass(Project.class);
config.addAnnotatedClass(Module.class);
config.addAnnotatedClass(Task.class);
config.configure("hibernate.cfg.xml");

new SchemaExport(config).create(true, true);

SessionFactory factory=config.buildSessionFactory();
Session session=factory.getCurrentSession();
session.beginTransaction();

Project p=new Project();
p.setProjectName("Hibernate Project");

Module m=new Module();
m.setProjectName("Spring AOP");
m.setModuleName("AOP");

Task t=new Task();
t.setProjectName("JAVA");
t.setModuleName("Collections");
t.setTaskName("ArrayList");



session.save(p);
session.save(m);
session.save(t);
session.getTransaction().commit();



t=null;
session=factory.openSession();
session.beginTransaction();
t=(Task)session.get(Task.class, 3);

System.out.println("Task Object "+t.getProjectName());

}}
------------------------------------------------------------------------------
<?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">oracle.jdbc.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">venkat</property>
        <property name="connection.password">venkat</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">2</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

        <!-- Enable Hibernate's current session context -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
<!--         <property name="hbm2ddl.auto">create</property> -->

<!--         <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!--         <mapping resource="org/hibernate/tutorial/domain/Person.hbm.xml"/> -->

    </session-factory>

</hibernate-configuration>