Saturday, April 21, 2012

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>

No comments:

Post a Comment