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_user"
)
publicclass User {
private
Integer id;
private
String name;
private
Card card;
@OneToOne
@JoinColumn
(name=
"card_id"
,unique=
true
)
//name是自定义关联外键的列名
public
Card getCard() {
returncard;
}
publicvoid setCard(Card card) {
this
.card = card;
}
@Id
@GeneratedValue
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
@Column
(name=
"name"
)
public
String getName() {
returnname;
}
publicvoid setName(Stringname) {
this
.name = name;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
@Entity
@Table
(name=
"t_card"
)
publicclass Card {
private
Integer id;
private
String num;
@Id
@GeneratedValue
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
@Column
(name=
"card_id"
)
public
String getNum() {
returnnum;
}
publicvoid setNum(Stringnum) {
this
.num = num;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
publicclass Group {
private
Integer id;
private
String name;
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
public
String getName() {
returnname;
}
publicvoid setName(Stringname) {
this
.name = name;
}
}
|
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
|
publicclass Person {
private
Integer id;
private
String name;
private
Integer age;
private
Group group;
public
Group getGroup() {
returngroup;
}
publicvoid setGroup(Groupgroup) {
this
.group = group;
}
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
public
String getName() {
return
name;
}
public
void
setName(Stringname) {
this
.name = name;
}
public
Integer getAge() {
return
age;
}
publicvoid setAge(Integer age){
this
.age = age;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
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"
/>
</
class
>
</
hibernate-mapping
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?
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"
/>
<
many-to-one
name
=
"group"
column
=
"group_id"
unique
=
"true"
not-null
=
"true"
/>
</
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
|
@Entity
@Table
(name=
"t_user"
)
publicclass User {
private
Integer id;
private
String name;
private
Card card;
@OneToOne
@JoinColumn
(name=
"card_id"
,unique=
true
)
//name主要是自定义关联外键的列名
public
Card getCard() {
returncard;
}
publicvoid setCard(Cardcard) {
this
.card = card;
}
@Id
@GeneratedValue
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
@Column
(name=
"name"
)
public
String getName() {
returnname;
}
publicvoid setName(Stringname) {
this
.name = name;
}
}
|
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
|
@Entity
@Table
(name=
"t_card"
)
publicclass Card {
private
Integer id;
private
String num;
private
User user;
@OneToOne
(mappedBy=
"card"
)
//mappedBy的意思是指定User中的card作为关联外键,否则User和Card都会出现外键
public
User getUser() {
returnuser;
}
publicvoid setUser(Useruser) {
this
.user = user;
}
@Id
@GeneratedValue
public
Integer getId() {
returnid;
}
publicvoid setId(Integerid) {
this
.id = id;
}
@Column
(name=
"card_id"
)
public
String getNum() {
returnnum;
}
publicvoid setNum(Stringnum) {
this
.num = num;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?
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"
/>
<
many-to-one
name
=
"group"
column
=
"group_id"
unique
=
"true"
not-null
=
"true"
/>
</
class
>
</
hibernate-mapping
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?
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"
/>
<!-- many-to-one这种配置会分别在两个表中都产生外键,造成数据的多余,通常我们采用one-to-one的形式在xml中配置 -->
<
many-to-one
name
=
"Person"
column
=
"person_id"
unique
=
"true"
/>
<!-- <one-to-onename="person"property-ref="group"/> -->
</
class
>
</
hibernate-mapping
>
|