Hibernate 제로 부터07_다 중 관계 (중간 표)

7697 단어
1. Hibernate 는 여러 쌍 의 필드 (중간 표 에 여러 필드 포함) 를 두 쌍 으로 나 누 었 다.다음 과 같이 구현:
    학생 클래스:
/**
 *    
 */
public class Student {
 private Long id;
 private String name;
 
 //               
 private Set<StudentCourseRelation> scr = new HashSet<StudentCourseRelation>();
 
 public Student() {
 }
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Set<StudentCourseRelation> getScr() {
  return scr;
 }
 public void setScr(Set<StudentCourseRelation> scr) {
  this.scr = scr;
 }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC   
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
 <hibernate-mapping>
  <class name="com.study.hibernate.domain.Student" table="t_student" catalog="db_hibernate">
   <id name="id" column="id" type="java.lang.Long">
    <generator  class="native"></generator>
   </id>
   
   <property name="name" column="name" type="java.lang.String"></property>
   
   
   <set name="scr" cascade="all">
    <key column="student_id"/>
    <one-to-many class="com.study.hibernate.domain.StudentCourseRelation"/>
   </set>
  </class>
 </hibernate-mapping>

    수업 종류:
/**
 *   
 */
public class Course {
 private Long id;
 private String name;
 
 //               
 private Set<StudentCourseRelation> scr = new HashSet<StudentCourseRelation>();
 
 public Course(){
  
 }
 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Set<StudentCourseRelation> getScr() {
  return scr;
 }
 public void setScr(Set<StudentCourseRelation> scr) {
  this.scr = scr;
 }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC   
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
 <hibernate-mapping>
  <class name="com.study.hibernate.domain.Course" table="t_course" catalog="db_hibernate">
   <id name="id" column="id" type="java.lang.Long">
    <generator  class="native"></generator>
   </id>
   <property name="name" column="name" type="java.lang.String"></property>
   <set name="scr" cascade="all">
    <key column="course_id"/>
    <one-to-many class="com.study.hibernate.domain.StudentCourseRelation"/>
   </set>
  </class>
 </hibernate-mapping>

    학생 과정 관련 유형:
/**
 *        
 */
public class StudentCourseRelation {
 private Long id;
 private Long sort;
 private Student students;
 private Course courses;
 
 public Long getId() {
  return id;
 }
 public void setId(Long id) {
  this.id = id;
 }
 
 public Long getSort() {
  return sort;
 }
 public void setSort(Long sort) {
  this.sort = sort;
 }
 public Student getStudents() {
  return students;
 }
 public void setStudents(Student students) {
  this.students = students;
 }
 public Course getCourses() {
  return courses;
 }
 public void setCourses(Course courses) {
  this.courses = courses;
 }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC   
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 
 <hibernate-mapping>
  <class name="com.study.hibernate.domain.StudentCourseRelation" table="t_student_course_relation" catalog="db_hibernate">
   <id name="id" column="id" type="java.lang.Long">
    <generator  class="native"></generator>
   </id>
   <property name="sort" column="sort" type="java.lang.Long"></property>
   <many-to-one name="students" class="com.study.hibernate.domain.Student" column="student_id"></many-to-one>
   <many-to-one name="courses" class="com.study.hibernate.domain.Course" column="course_id"></many-to-one>
  </class>
 </hibernate-mapping>

    테스트 코드:
/**
  *     
  */
 @Test
 public void insertTest(){
  Configuration configuration = new Configuration().configure();
  ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
  SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  Session session = sessionFactory.openSession();
  Transaction transaction = session.beginTransaction();
  
  Student stu1 = new Student();
  stu1.setName("zhangsan");
  
  Course c1 = new Course();
  c1.setName("English");
  
  session.persist(stu1);
  session.persist(c1);
  
  StudentCourseRelation scr = new StudentCourseRelation();
  scr.setStudents(stu1);
  scr.setCourses(c1);
  session.persist(scr);
  transaction.commit();
  session.close();
  sessionFactory.close();
  
  //  
//  Hibernate: /* insert com.study.hibernate.domain.Student */ insert into db_hibernate.t_student (name) values (?)
//  Hibernate: /* insert com.study.hibernate.domain.Course */ insert into db_hibernate.t_course (name) values (?)
//  Hibernate: /* insert com.study.hibernate.domain.StudentCourseRelation */ insert into db_hibernate.t_student_course_relation (student_id, course_id) values (?, ?)
 }
 /**
  *     
  */
 @Test
 public void queryTest(){
  Configuration configuration = new Configuration().configure();
  ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
  SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  Session session = sessionFactory.openSession();
  Transaction transaction = session.beginTransaction();
  
  
  Student stu = (Student) session.load(Student.class, new Long(2));
  Set<StudentCourseRelation> scr =  stu.getScr();
  for (StudentCourseRelation studentCourseRelation : scr) {
   System.out.println(studentCourseRelation.getCourses().getId());
   System.out.println(studentCourseRelation.getCourses().getName());
  }
  
  
  transaction.commit();
  session.close();
  sessionFactory.close();
 }

    결과:
Hibernate: /* load com.study.hibernate.domain.Student */ select student0_.id as id1_1_0_, student0_.name as name2_1_0_ from db_hibernate.t_student student0_ where student0_.id=?
Hibernate: /* load one-to-many com.study.hibernate.domain.Student.scr */ select scr0_.student_id as student_3_1_1_, scr0_.id as id1_2_1_, scr0_.id as id1_2_0_, scr0_.sort as sort2_2_0_, scr0_.student_id as student_3_2_0_, scr0_.course_id as course_i4_2_0_ from db_hibernate.t_student_course_relation scr0_ where scr0_.student_id=?
Hibernate: /* load com.study.hibernate.domain.Course */ select course0_.id as id1_0_0_, course0_.name as name2_0_0_ from db_hibernate.t_course course0_ where course0_.id=?
2:English

좋은 웹페이지 즐겨찾기