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 >
|
相关推荐
Hibernate 一对一外键单向...Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。
Hibernate关联关系映射 单向关联 │ ├─ 一对一外键单向关联...├─ 一对一外键双向关联 ├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向
建立对SQL语句的映射是Hibernate框架操作数据库的主要手段,这里我们列举实例来为大家讲解Java的Hibernate框架中一对多的单向和双向关联映射
hibernate核心,一对多,多对多映射讲解,看了就完全搞明白了
009 一对一 主键关联映射_单向 010 一对一 主键关联映射_双向 011 一对一 唯一外键关联映射_单向 012 一对一 唯一外键关联映射_双向 013 session_flush 014 一对多关联映射 单向 015 一对多关联映射 双向 016 多对多...
1.共享主键的一对一关联映射: @Entity @Table(name="Test_Body") public class Body { private Integer id; private Heart heart; @Id public Integer getId() { return id; } public void setId(Integer id...
hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射(单向关联Person---->IdCard) 10 hibernate一对一唯一外键关联映射(双向关联Person<---->IdCard) 11 session ...
Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚
7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...
12.4.5 多对一单向关联的annotation注解实现538 12.4.6 一对多双向关联的annotation注解实现540 12.4.7 一对多双向自身关联的annotation注解实现542 12.4.8 多对多单向关联的annotation注解实现543 12.4.9 多对多...
ESho1.0版本主要实现项目的创建,数据库的创建与ORM映射. [创建项目与数据库] 1."电子商城系统"EShop应用项目的创建 ...12.配置订单到购物车的一对一单向关联关系 13.配置购物车到商品的多对多单向关联关系
7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...
7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. ...
7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...
7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多...
8.2.2. 一对一(one to one) 8.2.3. 一对多(one to many) 8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一...
一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many ...
7.2 映射一对多双向关联关系 7.2.1 元素的inverse属性 7.2.2 级联删除 7.2.3 父子关系 7.3 映射一对多双向自身关联关系 7.4 改进持久化类 7.5 小结 7.6 思考题 第8章 通过Hibernate操纵对象(上) ...