008关联映射:
Ø 多对一 --- many-to-one
Ø 一对多 --- one-to-many
Ø 一对一 --- one-to-one
Ø 多对多 --- many-to-many
008多对一 关联映射 --- many-to-one
场景:用户和组;从用户角度来,多个用户属于一个组(多对一 关联)
使用hibernate开发的思路:先建立对象模型(领域模型),把实体抽取出来。
目前两个实体:用户和组两个实体,多个用户属于一个组,那么一个用户都会对应于一个组,所以用户实体中应该有一个持有组的引用。
对象模型图:
关系模型:
关联映射的本质:
将关联关系映射到数据库,所谓的关联关系是对象模型在内存中一个或多个引用。
User实体类:
public class User {
private int id;
private String name;
private Group group;
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Group实体类:
public class Group {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
实体类建立完后,开始创建映射文件,先建立简单的映射文件:
Group实体类的映射文件:
<hibernate-mapping>
<class name="com.wjt276.hibernate.Group" table="t_group">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
User实体类的映射文件:
<hibernate-mapping>
<class name="com.wjt276.hibernate.User" table="t_user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<!--<many-to-one> 关联映射 多对一的关系
name:是维护的属性(User.group),这样表示在多的一端表里加入一个字段名称为group,
但group与SQL中的关键字重复,所以需要重新命名字段(column="groupid").
这样这个字段(groupid)会作为外键参照数据库中group表(t_group也叫一的一端),也就是就在多的一
端加入一个外键指向一的一端。
-->
<many-to-one name="group" column="groupid"/>
</class>
</hibernate-mapping>
※<many-to-one>标签※:
例如:<many-to-one name="group" column="groupid"/>
<many-to-one> 关联映射 多对一的关系
name:是维护的属性(User.group),这样表示在多的一端表里加入一个字段名称为group,但group与SQL中的关键字重复,所以需要重新命名字段(column="groupid").这样这个字段(groupid)会作为外键参照数据库中group表(t_group也叫一的一端),也就是就在多的一端加入一个外键指向一的一端。
这样导出至数据库会生成下列语句:
alter table t_user drop foreign key FKCB63CCB695B3B5AC
drop table if exists t_group
drop table if exists t_user
create table t_group (id integer not null auto_increment, name varchar(255), primary key (id))
create table t_user (id integer not null auto_increment, name varchar(255), groupid integer, primary key (id))
alter table t_user add index FKCB63CCB695B3B5AC (groupid), add constraint FKCB63CCB695B3B5AC foreign key (groupid) references t_group (id)
- 大小: 3.2 KB
- 大小: 3.7 KB
分享到:
相关推荐
关联映射的本质: 将关联关系映射到数据库,所谓的...<many-to-one>会在多的一端加入一个外键,指向一的一端,这个外键是由<many-to-one> 中的column属性定义的,如果忽略了这个属性那么默认的外键与实体的属性一致
008 多对一 关联映射 009 一对一 主键关联映射_单向 010 一对一 主键关联映射_双向 011 一对一 唯一外键关联映射_单向 012 一对一 唯一外键关联映射_双向 013 session_flush 014 一对多关联映射 单向 015 一对多关联...
NULL 博文链接:https://yinxiaoyong.iteye.com/blog/426752
第一阶段的工程 博文链接:https://selvemen.iteye.com/blog/458615
hibernate开发的关系: one-to-one,many-to-one,many-to-many学习经验总结
hibernate多对一关联映射(Hibernate_Many2One) 7 hibernate一对一主键关联映射(单向关联Person---->IdCard) 8 hibernate一对一主键关联映射(双向关联Person<---->IdCard) 9 hibernate一对一唯一外键关联映射...
多对一(Many-to-One) 使用@ManyToOne批注来实现多对一关联。 @ManyToOne批注有一个名为targetEntity的参数,该参数定义了目标实体名,通常不需要定义该参数,因为在大部分情况下默认值(表示关联关系的属性类型)就...
该资源包括了hibernate开发中的常见关联映射的源代码,其中包括:one2one,one2many,many2many等,还有继承映射,inverse,fetch,cascade关键字的应用等,对初学者来说,是很实用的,难度适中,很好理解
图14-11ClassD与ClassA为多对一多态关联关系ClassA、ClassB和ClassC构成了一棵继承关系树,如果继承关系树的根类对应一个表,或者每个类对应一个表,那么可以按以下方式映射ClassD的a属性:<many-to-onename="a...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...
一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
值集合和多对多关联(Collections of Values and Many-To-Many Associations) 14 一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 ...