1、Hibernate多对多(中间表含多个字段)的把多对多拆分为两个一对多。实现如下:
学生类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* 学生表
*/
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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<
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
>
|
课程类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/**
* 课程
*/
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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<
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
>
|
学生课程关联类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/**
* 学生课程关联表
*/
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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<
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
>
|
测试代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/**
* 插入数据
*/
@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 (?, ?)
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/**
* 查询数据
*/
@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();
}
|
结果:
1
2
3
4
|
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
|