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 ...
随机推荐
- pytroch nn.Module源码解析(1)
今天在写一个分类网络时,要使用nn.Sequential中的一个模块,因为nn.Sequential中模块都没有名字,我一时竟无从下笔.于是决定写这篇博客梳理pytorch的nn.Module类,看完 ...
- JQuery实现旋转轮播图
css部分 <style> *{ margin:; padding:; } .container{ width:100%; height:353px; margin-top: 20px; ...
- 机器学习——KNN算法(k近邻算法)
一 KNN算法 1. KNN算法简介 KNN(K-Nearest Neighbor)工作原理:存在一个样本数据集合,也称为训练样本集,并且样本集中每个数据都存在标签,即我们知道样本集中每一数据与所属分 ...
- web项目部署到服务器中浏览器中显示乱码
项目部署之后浏览器打开查看时页面乱码 这里可能需要修改一下tomcat配置文件,首先找到Tomcat的安装路径下的conf/server.xml文件,找到之后可以CTRL+F搜索如下的内容: < ...
- 离校登记网页项目个人总结(Alpha阶段)
个人小结 在Alpha阶段,我和我的小团队六人,经过六天的努力完成了我们最初需求分析里的基本功能,在这里为我们团队的成功表示祝贺.在这个过程中,对于自己的表现觉得既有做的好的方面,也有很多不足需要改进 ...
- 权限组件之rbac
rbac:基于角色的权限访问控制(Role-Based Access Control). rbac的主要流程:给每个角色赋予不同的权限,是这个角色的员工都有这个角色的所有权限.一个角色可以有多个人员担 ...
- FBOSS: Building Switch Software at Scale
BOSS: 大规模环境下交换机软件构建 本文为SIGCOMM 2018 论文,由Facebook提供. 本文翻译了论文的关键内容. 摘要: 在网络设备(例如交换机和路由器)上运行的传统软件,通常是由供 ...
- libguestfs手册(2):guestfish command
添加一个drive:guestfs_add_drive_opts add-drive filename [readonly:true|false] [format:..] [iface:..] [na ...
- PuppeteerSharp+AngleSharp的爬虫实战之汽车之家数据抓取
参考了DotNetSpider示例, 感觉DotNetSpider太重了,它是一个比较完整的爬虫框架. 对比了以下各种无头浏览器,最终采用PuppeteerSharp+AngleSharp写一个爬虫示 ...
- 对JS闭包和函数作用域的问题的深入讨论,如何理解JS闭包和函数作用域链?
首先先引用<JavaScript权威指南>里面的一句话来开始我的博客:函数的执行依赖于变量作用域,这个作用域是在函数定义时决定的,而不是函数调用时决定的. 因此,就出现了如下的几串代码: ...