Hibernate 제로 부터07_다 중 관계 (중간 표)
학생 클래스:
/**
*
*/
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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.