============
package myhb;
import java.util.Set;
public class Course {
private int courseId;
private String courseName;
private int duration;
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 int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
}
Student.java
=============
package myhb;
import java.util.Set;
public class Student {
private int studentId;
private String studentName;
private int marks;
private Set courses;
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;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
public Set getCourses() {
return courses;
}
public void setCourses(Set courses) {
this.courses = courses;
}
}
course.hbm.xml
==============
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="myhb.Course" table="courses">
<id name="courseId" column="courseid" ></id>
<property name="courseName" column="coursename" length="20" ></property>
<property name="duration" ></property>
<set name="students" inverse="false" cascade="all" table="students_courses">
<key column="course_id" ></key>
<many-to-many class="myhb.Student" column="student_id "></many-to-many>
</set>
</class>
student.hbm.xml
================
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="myhb.Student" table="student">
<id name="studentId" column="studentid" ></id>
<property name="studentName" column="studentname" length="20" ></property>
<property name="marks" ></property>
<set name="courses" cascade="all" table="students_courses">
<key column="student_id "></key>
<many-to-many class="myhb.Course" column="course_id" ></many-to-many>
</set>
</class>
</hibernate-mapping>
<?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>
<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property name="hibernate.connection.url">jdbc:derby://localhost:1527/technology</property>
<property name="hibernate.connection.username">app</property>
<property name="hibernate.connection.password">app</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="student.hbm.xml"/>
<mapping resource="course.hbm.xml"/>
</session-factory>
</hibernate-configuration>
OurLogic.java
=============
package myhb;
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;
public class OurLogic {
public static void main(String args[])
{
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Student s1=new Student();
s1.setStudentId(100);
s1.setStudentName("James");
s1.setMarks(98);
Student s2=new Student();
s2.setStudentId(101);
s2.setStudentName("Lee");
s2.setMarks(99);
Course c1=new Course();
c1.setCourseId(500);
c1.setCourseName("Hibernate");
c1.setDuration(7);
Course c2=new Course();
c2.setCourseId(501);
c2.setCourseName("Java");
c2.setDuration(30);
Set s =new HashSet();
s.add(c1);
s.add(c2);
s1.setCourses(s);
s2.setCourses(s);
Transaction tx = session.beginTransaction();
session.save(s1);
session.save(s2);
tx.commit();
session.close();
System.out.println("Many To Many Bi-Directional is Done..!!");
factory.close();
}
}
No comments:
Post a Comment