web进修之—Hibernate 关系映射(3)
概述
Hibernate的关系映射是Hibernate使用的难点或者是重点(别担心,不考试哦~),按照不同的分类方式可以对这些映射关系做一个分类,如:
按对象对应关系分:
- 一对一
- 多对一/一对多
- 多对多
按对象对应关系的方向分:
- 单向
- 双向
按是否使用连接表分(当然了像多对多是必须使用连接表的):
- 使用连接表
- 不使用连接表
在使用Hibernate中常常不会分得这么仔细,常常是集中分类方式糅合起来使用,但是这个分类可以帮助我们理解Hibernate中的映射关系。
在进行详细的介绍之前,首先需要明确几个点:
- Hibernate纷繁复杂的映射关系只是一种面向对象的思维方式,在数据库中就是体现在外键上关联上,所以理解了数据库中的外键关联,再回过头来看映射关系就柳暗花明了;
- index在数据库中的作用:为了提高查询该列的效率;
- constrain的作用:对该列的值进行约束(废话。。。),各个取值的含义如下
MUL:该列的值可以重复
- UNI:该列不能有重复的值
- FK:外键,一般定义的时候会有参考哪一个表的哪一个字段
代码
废话少说,是时候展现真正的代码啦
先看两个类Person,Address(你没看错,这是官方用来举例子的两个类)
- public class Address {
- private long id;
- private Set<Person> people;
- private Person person;
- // setter,getter
- }
- public class Person {
- private long id;
- private Address address;
- private Set<Address> addressList;
- // setter,getter
- }
public class Address {
private long id;
private Set<Person> people;
private Person person;// setter,getter
}
public class Person {
private long id;
private Address address;
private Set<Address> addressList;// setter,getter
}
各种映射关系的配置文件person.hbm.xml,里面包括单向关联的配置(在靠前面),包含双向关联的配置(紧随单向之后)
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.lep.hibernate.model" >
- <class name="Person" table="person">
- <id name="id" column="person_id">
- <generator class="increment">
- </generator>
- </id>
- <!--单向关联 start-->
- <!--多对一是一对多的反向关联,一对多、多对一就构成了多对多-->
- <!--多对一,只需要在many的一方使用many-to-one进行映射即可,和其他id、property一样name表示model里面的属性名称-->
- <!--<many-to-one name="address" column="address_id" not-null="true" />-->
- <!--一对一,基于外键,把many-to-one的unique设置为true之后就是单向一对一,在数据库中的体现就是这个外键的值是唯一(key=UNI,如果没有设置unique的话key=mul)的,那也就是只能对应一个Address-->
- <!--有not-null约束的时候注意save对象的顺序,如果没有不用注意顺序hibernate会在保存了关联的对象之后,update到当前对象对应的表-->
- <!--<many-to-one name="address" column="address_id" not-null="true" unique="true" />-->
- <!--一对一,基于主键,将主键作为外键的时候,关联的对象和本对象的save顺序是有关系的,要先savereference的对象,然后在保存本对象-->
- <!-- constrained说明主键上存在一个约束,即外键,参考address-->
- <!--<id name="id" column="person_id">-->
- <!--<generator class="foreign">-->
- <!--<param name="property">address</param>-->
- <!--</generator>-->
- <!--</id>-->
- <!--<one-to-one name="address" constrained="true" />-->
- <!--一对多-->
- <set name="addressList">
- <key column="person_id" >
- </key>
- <one-to-many class="Address"></one-to-many>
- </set>
- <!--单向关联 end-->
- <!--单向关联——基于连接表 start-->
- <!--使用多对多和join的时候会产生中间表-->
- <!--一对多使用many-to-many会产生一张中间表-->
- <!--<set name="addressList" table="per_addr">-->
- <!--<key column="person_id"></key>-->
- <!--<many-to-many column="address_id" class="Address" unique="true" />-->
- <!--</set>-->
- <!--多对一-->
- <!--<join table="per_addr" optional="true">-->
- <!--<key column="peron_id"></key>-->
- <!--<many-to-one name="address" column="address_id" not-null="true"></many-to-one>-->
- <!--</join>-->
- <!--一对一-->
- <!--<join table="per_addr" optional="true">-->
- <!--<key column="person_id"></key>-->
- <!--<many-to-one name="address" column="address_id" not-null="true" unique="true" />-->
- <!--</join>-->
- <!--多对多-->
- <!--<set name="addressList" table="per_addr">-->
- <!--<key column="person_id"></key>-->
- <!--<many-to-many column="address_id" class="Address"></many-to-many>-->
- <!--</set>-->
- <!--单向关联——基于连接表 end-->
- <!--双向关联 start-->
- <!--多对一/一对多-->
- <!--<many-to-one name="address" column="address_id" not-null="true" />-->
- <!--一对一,基于外键关联-->
- <!--<many-to-one name="address" column="address_id" unique="true" not-null="true" />-->
- <!--一对一,基于主键关联-->
- <!--<one-to-one name="address" />-->
- <!--双向关联 end-->
- <!--双向关联,使用连接表 start-->
- <!--多对一/一对多-->
- <!--<set name="addressList" table="per_addr">-->
- <!--<key column="person_id"></key>-->
- <!--<many-to-many class="Address" column="address_id" unique="true" />-->
- <!--</set>-->
- <!--一对一,必须指定column-->
- <!--<join table="per_addr" optional="true">-->
- <!--<key column="person_id" unique="true"></key>-->
- <!--<many-to-one name="address" column="address_id" class="Address" unique="true" />-->
- <!--</join>-->
- <!--多对多-->
- <!--<set name="addressList" table="per_addr">-->
- <!--<key column="person_id"></key>-->
- <!--<many-to-many column="address_id" class="Address"></many-to-many>-->
- <!--</set>-->
- <!--双向关联,使用连接表 end-->
- </class>
- </hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="org.lep.hibernate.model" >
<class name="Person" table="person">
<id name="id" column="person_id">
<generator class="increment">
</generator>
</id>
<!--单向关联 start-->
<!--多对一是一对多的反向关联,一对多、多对一就构成了多对多--><!--多对一,只需要在many的一方使用many-to-one进行映射即可,和其他id、property一样name表示model里面的属性名称-->
<!--<many-to-one name="address" column="address_id" not-null="true" />--><!--一对一,基于外键,把many-to-one的unique设置为true之后就是单向一对一,在数据库中的体现就是这个外键的值是唯一(key=UNI,如果没有设置unique的话key=mul)的,那也就是只能对应一个Address-->
<!--有not-null约束的时候注意save对象的顺序,如果没有不用注意顺序hibernate会在保存了关联的对象之后,update到当前对象对应的表-->
<!--<many-to-one name="address" column="address_id" not-null="true" unique="true" />--><!--一对一,基于主键,将主键作为外键的时候,关联的对象和本对象的save顺序是有关系的,要先savereference的对象,然后在保存本对象-->
<!-- constrained说明主键上存在一个约束,即外键,参考address-->
<!--<id name="id" column="person_id">-->
<!--<generator class="foreign">-->
<!--<param name="property">address</param>-->
<!--</generator>-->
<!--</id>-->
<!--<one-to-one name="address" constrained="true" />--><!--一对多-->
<set name="addressList">
<key column="person_id" >
</key>
<one-to-many class="Address"></one-to-many>
</set><!--单向关联 end-->
<!--单向关联——基于连接表 start-->
<!--使用多对多和join的时候会产生中间表-->
<!--一对多使用many-to-many会产生一张中间表-->
<!--<set name="addressList" table="per_addr">-->
<!--<key column="person_id"></key>-->
<!--<many-to-many column="address_id" class="Address" unique="true" />-->
<!--</set>--><!--多对一-->
<!--<join table="per_addr" optional="true">-->
<!--<key column="peron_id"></key>-->
<!--<many-to-one name="address" column="address_id" not-null="true"></many-to-one>-->
<!--</join>--><!--一对一-->
<!--<join table="per_addr" optional="true">-->
<!--<key column="person_id"></key>-->
<!--<many-to-one name="address" column="address_id" not-null="true" unique="true" />-->
<!--</join>--><!--多对多-->
<!--<set name="addressList" table="per_addr">-->
<!--<key column="person_id"></key>-->
<!--<many-to-many column="address_id" class="Address"></many-to-many>-->
<!--</set>-->
<!--单向关联——基于连接表 end--><!--双向关联 start-->
<!--多对一/一对多-->
<!--<many-to-one name="address" column="address_id" not-null="true" />--><!--一对一,基于外键关联-->
<!--<many-to-one name="address" column="address_id" unique="true" not-null="true" />--><!--一对一,基于主键关联-->
<!--<one-to-one name="address" />--><!--双向关联 end-->
<!--双向关联,使用连接表 start-->
<!--多对一/一对多-->
<!--<set name="addressList" table="per_addr">-->
<!--<key column="person_id"></key>-->
<!--<many-to-many class="Address" column="address_id" unique="true" />-->
<!--</set>--><!--一对一,必须指定column-->
<!--<join table="per_addr" optional="true">-->
<!--<key column="person_id" unique="true"></key>-->
<!--<many-to-one name="address" column="address_id" class="Address" unique="true" />-->
<!--</join>--><!--多对多-->
<!--<set name="addressList" table="per_addr">-->
<!--<key column="person_id"></key>-->
<!--<many-to-many column="address_id" class="Address"></many-to-many>-->
<!--</set>--><!--双向关联,使用连接表 end-->
</class>
</hibernate-mapping>
address.hbm.xml(只有双向关联的配置)
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.lep.hibernate.model" >
- <class name="Address" table="address">
- <id name="id" column="address_id">
- <generator class="increment"/>
- </id>
- <!--双向关联 start-->
- <!--多对一/一对多-->
- <!--<set name="people" inverse="true">-->
- <!--<key column="address_id"></key>-->
- <!--<one-to-many class="Person" />-->
- <!--</set>-->
- <!--一对一,基于外键关联-->
- <!--<one-to-one name="person" constrained="true"/>-->
- <!--一对一,基于主键关联-->
- <!--<id name="id" column="person_id">-->
- <!--<generator class="foreign">-->
- <!--<param name="property">person</param>-->
- <!--</generator>-->
- <!--</id>-->
- <!--<one-to-one name="person" constrained="true"/>-->
- <!--双向关联 end-->
- <!--双向关联,使用连接表 start-->
- <!--多对一/一对多-->
- <!--<join table="per_addr" inverse="true" optional="true">-->
- <!--<key column="address_id"></key>-->
- <!--<many-to-one name="person" column="person_id" not-null="true"></many-to-one>-->
- <!--</join>-->
- <!--一对一,必须指定column-->
- <!--<join table="per_addr" inverse="true" optional="true">-->
- <!--<key column="address_id" unique="true"></key>-->
- <!--<many-to-one name="person" column="person_id" unique="true" />-->
- <!--</join>-->
- <!--多对多-->
- <!--<set name="people" inverse="true" table="per_addr">-->
- <!--<key column="address_id"></key>-->
- <!--<many-to-many column="person_id" class="Person"></many-to-many>-->
- <!--</set>-->
- <!--双向关联,使用连接表 end-->
- </class>
- </hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="org.lep.hibernate.model" >
<class name="Address" table="address">
<id name="id" column="address_id">
<generator class="increment"/>
</id><!--双向关联 start-->
<!--多对一/一对多-->
<!--<set name="people" inverse="true">-->
<!--<key column="address_id"></key>-->
<!--<one-to-many class="Person" />-->
<!--</set>--><!--一对一,基于外键关联-->
<!--<one-to-one name="person" constrained="true"/>--><!--一对一,基于主键关联-->
<!--<id name="id" column="person_id">-->
<!--<generator class="foreign">-->
<!--<param name="property">person</param>-->
<!--</generator>-->
<!--</id>-->
<!--<one-to-one name="person" constrained="true"/>--><!--双向关联 end-->
<!--双向关联,使用连接表 start-->
<!--多对一/一对多-->
<!--<join table="per_addr" inverse="true" optional="true">-->
<!--<key column="address_id"></key>-->
<!--<many-to-one name="person" column="person_id" not-null="true"></many-to-one>-->
<!--</join>--><!--一对一,必须指定column-->
<!--<join table="per_addr" inverse="true" optional="true">-->
<!--<key column="address_id" unique="true"></key>-->
<!--<many-to-one name="person" column="person_id" unique="true" />-->
<!--</join>--><!--多对多-->
<!--<set name="people" inverse="true" table="per_addr">-->
<!--<key column="address_id"></key>-->
<!--<many-to-many column="person_id" class="Person"></many-to-many>-->
<!--</set>--><!--双向关联,使用连接表 end-->
</class>
</hibernate-mapping>
试验过这些配置之后,我们可以总结出:
- 一对一可以通过主键关联,也可以使用外键关联
- 通过使用unique属性可以将多(many)的关系变为一(one)的关系,比如一对一,可以使用many-to-one unique=”true”
- 在使用many-to-many或者使用join的时候才会产生连接表(中间表),可以根据实际情况来决定是否采用产生中间表的配置(比如查询的性能,业务逻辑的需要)
- 如果是双向关系,在一边配置之后,另一边使用inverse=”true”来告诉Hibernate有谁来控制
- 推荐外键设置not null,这样设置后就要注意保存对象的顺序了,比如person对address单向关联(Person有一个属性Address),那么person表里面就会有一个address_id的外键,如果这个外键设为not null,那么save的时候应该先save address,再save person
- Person p = new Person();
- Address addr = new Address();
- p.setAddress(addr);
- // 正确
- session.beginTransaction();
- session.save(address);
- session.save(p);
- session.getTransaction().commit();
- // 运行会报错,因为外键address设为了not null,在save p的时候,address_id还没有值,所以不满足非空的约束
- session.beginTransaction();
- session.save(p);
- session.save(address);
- session.getTransaction().commit();
Person p = new Person();
Address addr = new Address();
p.setAddress(addr);// 正确
session.beginTransaction();
session.save(address);
session.save(p);
session.getTransaction().commit();// 运行会报错,因为外键address设为了not null,在save p的时候,address_id还没有值,所以不满足非空的约束
session.beginTransaction();
session.save(p);
session.save(address);
session.getTransaction().commit();
因为外键address设为了not null,在save p的时候,address_id还没有值,所以不满足非空的约束
如果说大家对Hibernate理解比较熟了,只是想看看具体配置文件怎么样写,那么到上面就可以了,如果还想探究一下Hibernate和数据库的对应关系那请继续,先看一个文件(HIbernate在建立上面的映射关系的时候产生的数据库语句)
- 单向
- 多对一
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
- Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
- 一对一
- 基于外键
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
- Hibernate: alter table person add constraint UK_o8tnkglv9n1eeqmo7de7em37n unique (address_id)
- Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
- 基于主键
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table person add index FK_acrq16tm1ioc620qk2nm5gwyg (person_id), add constraint FK_acrq16tm1ioc620qk2nm5gwyg foreign key (person_id) references address (address_id)
- 一对多
- Hibernate: create table address (address_id bigint not null, person_id bigint, primary key (address_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table address add index FK_5k57pkctki2o1wpmk2880r74j (person_id), add constraint FK_5k57pkctki2o1wpmk2880r74j foreign key (person_id) references person (person_id)
- 单向——中间表
- 一对多
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table per_addr add constraint UK_8v3twe5k7nlb8wcjqvcpydab6 unique (address_id)
- Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
- Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
- 多对一
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table per_addr (peron_id bigint not null, address_id bigint not null, primary key (peron_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table per_addr add index FK_7xqe3kidwvogwcohihqla5ehv (peron_id), add constraint FK_7xqe3kidwvogwcohihqla5ehv foreign key (peron_id) references person (person_id)
- Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
- 一对一
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table per_addr add constraint UK_8v3twe5k7nlb8wcjqvcpydab6 unique (address_id)
- Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
- Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
- 多对多
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
- Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
- 双向
- 多对一/一对多
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
- Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
- 一对一
- 基于外键
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
- Hibernate: alter table person add constraint UK_o8tnkglv9n1eeqmo7de7em37n unique (address_id)
- Hibernate: alter table address add index FK_scpdoha0q1mmbp5f9lojr3s9x (address_id), add constraint FK_scpdoha0q1mmbp5f9lojr3s9x foreign key (address_id) references person (person_id)
- Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
- 基于主键
- Hibernate: create table address (person_id bigint not null, primary key (person_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table address add index FK_5k57pkctki2o1wpmk2880r74j (person_id), add constraint FK_5k57pkctki2o1wpmk2880r74j foreign key (person_id) references person (person_id)
- 双向——中间表
- 多对一/一对多
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table per_addr (address_id bigint not null, person_id bigint not null, primary key (person_id, address_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
- Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
- 一对一
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table per_addr (person_id bigint not null, address_id bigint, primary key (address_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table per_addr add constraint UK_hdwakolgq6oelbfuallvfbcn4 unique (person_id)
- Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
- Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
- 多对多
- Hibernate: create table address (address_id bigint not null, primary key (address_id))
- Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))
- Hibernate: create table person (person_id bigint not null, primary key (person_id))
- Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
- Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
单向
多对一
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)一对一
基于外键
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
Hibernate: alter table person add constraint UK_o8tnkglv9n1eeqmo7de7em37n unique (address_id)
Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)基于主键
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table person add index FK_acrq16tm1ioc620qk2nm5gwyg (person_id), add constraint FK_acrq16tm1ioc620qk2nm5gwyg foreign key (person_id) references address (address_id)一对多
Hibernate: create table address (address_id bigint not null, person_id bigint, primary key (address_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table address add index FK_5k57pkctki2o1wpmk2880r74j (person_id), add constraint FK_5k57pkctki2o1wpmk2880r74j foreign key (person_id) references person (person_id)单向——中间表
一对多
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table per_addr add constraint UK_8v3twe5k7nlb8wcjqvcpydab6 unique (address_id)
Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)多对一
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table per_addr (peron_id bigint not null, address_id bigint not null, primary key (peron_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table per_addr add index FK_7xqe3kidwvogwcohihqla5ehv (peron_id), add constraint FK_7xqe3kidwvogwcohihqla5ehv foreign key (peron_id) references person (person_id)
Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)一对一
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table per_addr add constraint UK_8v3twe5k7nlb8wcjqvcpydab6 unique (address_id)
Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)多对多
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)双向
多对一/一对多
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
一对一
基于外键
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))
Hibernate: alter table person add constraint UK_o8tnkglv9n1eeqmo7de7em37n unique (address_id)
Hibernate: alter table address add index FK_scpdoha0q1mmbp5f9lojr3s9x (address_id), add constraint FK_scpdoha0q1mmbp5f9lojr3s9x foreign key (address_id) references person (person_id)
Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)基于主键
Hibernate: create table address (person_id bigint not null, primary key (person_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table address add index FK_5k57pkctki2o1wpmk2880r74j (person_id), add constraint FK_5k57pkctki2o1wpmk2880r74j foreign key (person_id) references person (person_id)双向——中间表
多对一/一对多
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table per_addr (address_id bigint not null, person_id bigint not null, primary key (person_id, address_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)一对一
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table per_addr (person_id bigint not null, address_id bigint, primary key (address_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table per_addr add constraint UK_hdwakolgq6oelbfuallvfbcn4 unique (person_id)
Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)多对多
Hibernate: create table address (address_id bigint not null, primary key (address_id))
Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))
Hibernate: create table person (person_id bigint not null, primary key (person_id))
Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
通过上面的SQL语句,我们可以得出以下几个结论
- 单向、双向是在对象层面上去考虑的,其实在数据库层面上是一致的,比如单向多对一和双向多对一在表结构上一样的
- 为了提高效率一般在(中间表)外键上都会创建index
Hibernate中的关系映射是最常用、也是最复杂的。在之前的点滴学习中对Hibernate的映射关系也是一知半解,这次特地花时间认真整理学习了一下,特别是对Hibernate和数据库中表的对应关系进行了认真的探究。
web进修之—Hibernate 关系映射(3)的更多相关文章
- web进修之—Hibernate 继承映射(5)
先看三个类的继承关系,Payment是父类,CashPayment和CreditCardPayment是Payment的子类: view plaincopy to clipboardprint p ...
- Hibernate学习笔记-Hibernate关系映射
1. 初识Hibernate——关系映射 http://blog.csdn.net/laner0515/article/details/12905711 2. Hibernate 笔记8 关系映射1( ...
- web进修之—Hibernate起步(1)(2)
想开始写博客了,尝试了CSDN和cnblog之后还是觉得cnblog更加简洁.专注(不过cnblog不支持搬家),所以把刚刚写的两篇学习博客链接放在这儿,这样这个系列也算是完整了: web进修之—Hi ...
- 【SSH 基础】浅谈Hibernate关系映射(4)
继上篇博客 多对多关联映射(单向) 多对多对象关系映射,须要增加一张新表完毕基本映射. Hibernate会自己主动生成中间表 Hibernate使用many-to-many标签来表示多对多的关联,多 ...
- Hibernate关系映射时出现的问题
在学习Hibernate框架的关系映射时,遇到了一个问题: INFO: HHH000422: Disabling contextual LOB creation as connection was n ...
- Hibernate关系映射之many-to-many
1.建表 2.创建实体类及映射文件 Student.java类 public class Student implements java.io.Serializable { // Fields pri ...
- hibernate关系映射
多对一:比如多个订单对应同一个用户,需要在订单表中添加一个用户的属性 订单类: private Integer orderId; private Date createTime; private Us ...
- Hibernate关系映射(注解)
1.类级别注解 @Entity 映射实体类 @Table 映射数句库表 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bea ...
- Hibernate关系映射(三) 多对多
一.使用用户User和Role实现多对多的示例 User.java,实现对Role的引用 package com.lxit.entity; import java.util.HashSet; impo ...
随机推荐
- CentOS7虚拟机安装VMware Tools
1.在VMware中点击安装VMware Tools 2.挂载光驱 mount /dev/cdrom /mnt 3.拷贝解压,注意是大小写敏感的,如果不知道文件名可以用ls查看. [root@loca ...
- 接口测试——postman & jmeter
新名词: 自动化测试:写代码帮你测试 接口:是一个抽象的概念,一种交互关系. 抓包:拦截请求. 接口测试:就是功能测试,比后者还简单. 需要有测试文档,包括项目.模块.URL.请求方式.参数.参数说明 ...
- CSS content换行实现字符点点点loading效果
CSS代码如下: dot { display: inline-block; height: 1em; line-height: 1; text-align: left; vertical-align: ...
- gridlayout代码注释
<div class="wrapper"> //定义一节或者一部分区域,它的css样式对应的css中class选择器的wrapper <div class=&qu ...
- 文本在div中始终垂直居中
如果是文本字数固定,在div中垂直居中,相信大家都会 这边分享个不固定文本在div中垂直居中的方法 html代码 <div class="box"> <div c ...
- webpack-dev-server和webpack-dev-middleware的区别
webpack-dev-server webpack-dev-server实际上相当于启用了一个express的Http服务器+调用webpack-dev-middleware.它的作用主要是用来伺服 ...
- 一步一步 copy163: 网易严选 ---- vue-cli
面试点 组件间通信 生命周期函数 路由 - 参数 - 重定向 vuex 参考 网易严选商城小程序全栈开发,域名备案中近期上线(mpvue+koa2+mysql) 小程序服务端源码地址 小程序源码地址 ...
- Revisiting Network Support for RDMA
重新审视RDMA的网络支持 本文为SIGCOMM 2018会议论文. 笔者翻译了该论文.由于时间仓促,且笔者英文能力有限,错误之处在所难免:欢迎读者批评指正. 本文及翻译版本仅用于学习使用.如果有任何 ...
- 使用Sublime Text 或 vs2017开发Node.js程序
在学习一门开发语言时,为了从简单的方式入手,有时候直接用Notepad开始敲代码.曾经我也这样干过,这样做简洁而不简单啊! 随着时间的流逝,人也变得懒惰起来,做事前总是想借助一些工具来搞事情.< ...
- Python实现实现基于最小二乘法的线性回归
下面展示利用Python实现基于最小二乘法的线性回归模型,同时不需要引入其他科学计算以及机器学习的库. 利用Python代码表示如下: #首先引入数据集x,和y的值的大小利用Python的数据结构:列 ...