基于外键的一对多关联映射!一对多,Group(组)对于Person(人),一个组可以有多个人!ok?
Hibernate主要有两种配置方法,一种是Annotations 一种是XML!
下面是Annotations的配置方法!
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
|
@Entity
@Table
(name=
"t_group"
)
publicclass Group {
private
Integer id;
private
String name;
private
Set<Person> persons=newHashSet<Person>();
//set不允许重复,最适合数据库模型
@Id
@GeneratedValue
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
@Column
(name=
"t_name"
)
public
String getName() {
returnname;
}
publicvoid setName(Stringname) {
this
.name = name;
}
@OneToMany
//加上@JoinColumn是为了防止Hibernate将一对多当做多对多来处理,产生中间表
@JoinColumn
(name=
"group_id"
)
public
Set<Person>getPersons() {
returnpersons;
}
publicvoidsetPersons(Set<Person> persons){
this
.persons = persons;
}
}
|
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
|
@Entity
@Table
(name=
"t_person"
)
publicclass Person {
private
Integer id;
private
String name;
private
Integer age;
@Id
@GeneratedValue
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
@Column
(name=
"t_name"
)
public
String getName() {
returnname;
}
publicvoid setName(Stringname) {
this
.name = name;
}
@Column
(name=
"t_age"
)
public
Integer getAge() {
returnage;
}
publicvoid setAge(Integerage) {
this
.age = age;
}
}
|
XML配置一对多的单向关联映射
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
publicclass Person {
private
Integer id;
private
String name;
private
Integer age;
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
public
String getName() {
returnname;
}
publicvoid setName(Stringname) {
this
.name = name;
}
public
Integer getAge() {
returnage;
}
publicvoid setAge(Integerage) {
this
.age = age;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
publicclass Group {
private
Integer id;
private
String name;
private
Set<Person> persons=newHashSet<Person>();
//set不允许重复,最适合数据库模型
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
public
String getName() {
returnname;
}
publicvoid setName(Stringname) {
this
.name = name;
}
public
Set<Person>getPersons() {
returnpersons;
}
publicvoidsetPersons(Set<Person> persons) {
this
.persons = persons;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
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
package
=
"csg.hibernate.entity"
>
<
class
name
=
"Group"
table
=
"t_group"
>
<
id
name
=
"id"
>
<
column
name
=
"id"
/>
<
generator
class
=
"native"
/>
</
id
>
<
property
name
=
"name"
/>
<
set
name
=
"persons"
>
<
key
column
=
"group_id"
/>
<
one-to-many
class
=
"csg.hibernate.entity.Person"
/>
</
set
>
</
class
>
</
hibernate-mapping
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?
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
package
=
"csg.hibernate.entity"
>
<
class
name
=
"Person"
table
=
"t_person"
>
<
id
name
=
"id"
>
<
column
name
=
"id"
/>
<
generator
class
=
"native"
/>
</
id
>
<
property
name
=
"name"
/>
<
property
name
=
"age"
/>
</
class
>
</
hibernate-mapping
>
|
Ok,一对多单向关联映射的Annotations和XML配置方法!
本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1568701,如需转载请自行联系原作者