概述

Hibernate的关系映射是Hibernate使用的难点或者是重点(别担心,不考试哦~),按照不同的分类方式可以对这些映射关系做一个分类,如:

按对象对应关系分:

  • 一对一
  • 多对一/一对多
  • 多对多

按对象对应关系的方向分:

  • 单向
  • 双向

按是否使用连接表分(当然了像多对多是必须使用连接表的):

  • 使用连接表
  • 不使用连接表

在使用Hibernate中常常不会分得这么仔细,常常是集中分类方式糅合起来使用,但是这个分类可以帮助我们理解Hibernate中的映射关系。

在进行详细的介绍之前,首先需要明确几个点:

  • Hibernate纷繁复杂的映射关系只是一种面向对象的思维方式,在数据库中就是体现在外键上关联上,所以理解了数据库中的外键关联,再回过头来看映射关系就柳暗花明了;
  • index在数据库中的作用:为了提高查询该列的效率;
  • constrain的作用:对该列的值进行约束(废话。。。),各个取值的含义如下
  • MUL:该列的值可以重复

  • UNI:该列不能有重复的值
  • FK:外键,一般定义的时候会有参考哪一个表的哪一个字段

代码

废话少说,是时候展现真正的代码啦

先看两个类Person,Address(你没看错,这是官方用来举例子的两个类)



  1. public class Address {

  2. private long id;

  3. private Set<Person> people;

  4. private Person person;
  5. // setter,getter

  6. }

  7. public class Person {

  8. private long id;

  9. private Address address;

  10. private Set<Address> addressList;
  11. // setter,getter

  12. }
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,里面包括单向关联的配置(在靠前面),包含双向关联的配置(紧随单向之后)



  1. <!DOCTYPE hibernate-mapping PUBLIC

  2. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

  3. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="org.lep.hibernate.model" >
  5. <class name="Person" table="person">

  6. <id name="id" column="person_id">

  7. <generator class="increment">

  8. </generator>

  9. </id>

  10. <!--单向关联 start-->

  11. <!--多对一是一对多的反向关联,一对多、多对一就构成了多对多-->
  12. <!--多对一,只需要在many的一方使用many-to-one进行映射即可,和其他id、property一样name表示model里面的属性名称-->

  13. <!--<many-to-one name="address" column="address_id" not-null="true" />-->
  14. <!--一对一,基于外键,把many-to-one的unique设置为true之后就是单向一对一,在数据库中的体现就是这个外键的值是唯一(key=UNI,如果没有设置unique的话key=mul)的,那也就是只能对应一个Address-->

  15. <!--有not-null约束的时候注意save对象的顺序,如果没有不用注意顺序hibernate会在保存了关联的对象之后,update到当前对象对应的表-->

  16. <!--<many-to-one name="address" column="address_id" not-null="true" unique="true" />-->
  17. <!--一对一,基于主键,将主键作为外键的时候,关联的对象和本对象的save顺序是有关系的,要先savereference的对象,然后在保存本对象-->

  18. <!-- constrained说明主键上存在一个约束,即外键,参考address-->

  19. <!--<id name="id" column="person_id">-->

  20. <!--<generator class="foreign">-->

  21. <!--<param name="property">address</param>-->

  22. <!--</generator>-->

  23. <!--</id>-->

  24. <!--<one-to-one name="address" constrained="true" />-->
  25. <!--一对多-->

  26. <set name="addressList">

  27. <key column="person_id" >

  28. </key>

  29. <one-to-many class="Address"></one-to-many>

  30. </set>
  31. <!--单向关联 end-->
  32. <!--单向关联——基于连接表 start-->

  33. <!--使用多对多和join的时候会产生中间表-->

  34. <!--一对多使用many-to-many会产生一张中间表-->

  35. <!--<set name="addressList" table="per_addr">-->

  36. <!--<key column="person_id"></key>-->

  37. <!--<many-to-many column="address_id" class="Address" unique="true" />-->

  38. <!--</set>-->
  39. <!--多对一-->

  40. <!--<join table="per_addr" optional="true">-->

  41. <!--<key column="peron_id"></key>-->

  42. <!--<many-to-one name="address" column="address_id" not-null="true"></many-to-one>-->

  43. <!--</join>-->
  44. <!--一对一-->

  45. <!--<join table="per_addr" optional="true">-->

  46. <!--<key column="person_id"></key>-->

  47. <!--<many-to-one name="address" column="address_id" not-null="true" unique="true" />-->

  48. <!--</join>-->
  49. <!--多对多-->

  50. <!--<set name="addressList" table="per_addr">-->

  51. <!--<key column="person_id"></key>-->

  52. <!--<many-to-many column="address_id" class="Address"></many-to-many>-->

  53. <!--</set>-->

  54. <!--单向关联——基于连接表 end-->
  55. <!--双向关联 start-->
  56. <!--多对一/一对多-->

  57. <!--<many-to-one name="address" column="address_id" not-null="true" />-->
  58. <!--一对一,基于外键关联-->

  59. <!--<many-to-one name="address" column="address_id" unique="true" not-null="true" />-->
  60. <!--一对一,基于主键关联-->

  61. <!--<one-to-one name="address" />-->
  62. <!--双向关联 end-->
  63. <!--双向关联,使用连接表 start-->
  64. <!--多对一/一对多-->

  65. <!--<set name="addressList" table="per_addr">-->

  66. <!--<key column="person_id"></key>-->

  67. <!--<many-to-many class="Address" column="address_id" unique="true" />-->

  68. <!--</set>-->
  69. <!--一对一,必须指定column-->

  70. <!--<join table="per_addr" optional="true">-->

  71. <!--<key column="person_id" unique="true"></key>-->

  72. <!--<many-to-one name="address" column="address_id" class="Address" unique="true" />-->

  73. <!--</join>-->
  74. <!--多对多-->

  75. <!--<set name="addressList" table="per_addr">-->

  76. <!--<key column="person_id"></key>-->

  77. <!--<many-to-many column="address_id" class="Address"></many-to-many>-->

  78. <!--</set>-->
  79. <!--双向关联,使用连接表 end-->

  80. </class>

  81. </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(只有双向关联的配置)



  1. <!DOCTYPE hibernate-mapping PUBLIC

  2. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

  3. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
  4. <hibernate-mapping package="org.lep.hibernate.model" >

  5. <class name="Address" table="address">

  6. <id name="id" column="address_id">

  7. <generator class="increment"/>

  8. </id>
  9. <!--双向关联 start-->
  10. <!--多对一/一对多-->

  11. <!--<set name="people" inverse="true">-->

  12. <!--<key column="address_id"></key>-->

  13. <!--<one-to-many class="Person" />-->

  14. <!--</set>-->
  15. <!--一对一,基于外键关联-->

  16. <!--<one-to-one name="person" constrained="true"/>-->
  17. <!--一对一,基于主键关联-->

  18. <!--<id name="id" column="person_id">-->

  19. <!--<generator class="foreign">-->

  20. <!--<param name="property">person</param>-->

  21. <!--</generator>-->

  22. <!--</id>-->

  23. <!--<one-to-one name="person" constrained="true"/>-->
  24. <!--双向关联 end-->
  25. <!--双向关联,使用连接表 start-->
  26. <!--多对一/一对多-->

  27. <!--<join table="per_addr" inverse="true" optional="true">-->

  28. <!--<key column="address_id"></key>-->

  29. <!--<many-to-one name="person" column="person_id" not-null="true"></many-to-one>-->

  30. <!--</join>-->
  31. <!--一对一,必须指定column-->

  32. <!--<join table="per_addr" inverse="true" optional="true">-->

  33. <!--<key column="address_id" unique="true"></key>-->

  34. <!--<many-to-one name="person" column="person_id" unique="true"  />-->

  35. <!--</join>-->
  36. <!--多对多-->

  37. <!--<set name="people" inverse="true" table="per_addr">-->

  38. <!--<key column="address_id"></key>-->

  39. <!--<many-to-many column="person_id" class="Person"></many-to-many>-->

  40. <!--</set>-->
  41. <!--双向关联,使用连接表 end-->
  42. </class>

  43. </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


  1. Person p = new Person();

  2. Address addr = new Address();

  3. p.setAddress(addr);
  4. // 正确

  5. session.beginTransaction();

  6. session.save(address);

  7. session.save(p);

  8. session.getTransaction().commit();
  9. // 运行会报错,因为外键address设为了not null,在save p的时候,address_id还没有值,所以不满足非空的约束

  10. session.beginTransaction();

  11. session.save(p);

  12. session.save(address);

  13. 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在建立上面的映射关系的时候产生的数据库语句)



  1. 单向

  2. 多对一

  3. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  4. Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))

  5. Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
  6. 一对一

  7. 基于外键

  8. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  9. Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))

  10. Hibernate: alter table person add constraint UK_o8tnkglv9n1eeqmo7de7em37n unique (address_id)

  11. Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
  12. 基于主键

  13. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  14. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  15. Hibernate: alter table person add index FK_acrq16tm1ioc620qk2nm5gwyg (person_id), add constraint FK_acrq16tm1ioc620qk2nm5gwyg foreign key (person_id) references address (address_id)
  16. 一对多

  17. Hibernate: create table address (address_id bigint not null, person_id bigint, primary key (address_id))

  18. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  19. Hibernate: alter table address add index FK_5k57pkctki2o1wpmk2880r74j (person_id), add constraint FK_5k57pkctki2o1wpmk2880r74j foreign key (person_id) references person (person_id)
  20. 单向——中间表

  21. 一对多

  22. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  23. Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))

  24. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  25. Hibernate: alter table per_addr add constraint UK_8v3twe5k7nlb8wcjqvcpydab6 unique (address_id)

  26. Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)

  27. Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
  28. 多对一

  29. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  30. Hibernate: create table per_addr (peron_id bigint not null, address_id bigint not null, primary key (peron_id))

  31. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  32. Hibernate: alter table per_addr add index FK_7xqe3kidwvogwcohihqla5ehv (peron_id), add constraint FK_7xqe3kidwvogwcohihqla5ehv foreign key (peron_id) references person (person_id)

  33. Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
  34. 一对一

  35. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  36. Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id))

  37. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  38. Hibernate: alter table per_addr add constraint UK_8v3twe5k7nlb8wcjqvcpydab6 unique (address_id)

  39. Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)

  40. Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
  41. 多对多

  42. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  43. Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))

  44. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  45. Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)

  46. Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
  47. 双向

  48. 多对一/一对多

  49. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  50. Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))

  51. Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)

  52. 一对一

  53. 基于外键

  54. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  55. Hibernate: create table person (person_id bigint not null, address_id bigint not null, primary key (person_id))

  56. Hibernate: alter table person add constraint UK_o8tnkglv9n1eeqmo7de7em37n unique (address_id)

  57. Hibernate: alter table address add index FK_scpdoha0q1mmbp5f9lojr3s9x (address_id), add constraint FK_scpdoha0q1mmbp5f9lojr3s9x foreign key (address_id) references person (person_id)

  58. Hibernate: alter table person add index FK_o8tnkglv9n1eeqmo7de7em37n (address_id), add constraint FK_o8tnkglv9n1eeqmo7de7em37n foreign key (address_id) references address (address_id)
  59. 基于主键

  60. Hibernate: create table address (person_id bigint not null, primary key (person_id))

  61. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  62. Hibernate: alter table address add index FK_5k57pkctki2o1wpmk2880r74j (person_id), add constraint FK_5k57pkctki2o1wpmk2880r74j foreign key (person_id) references person (person_id)
  63. 双向——中间表

  64. 多对一/一对多

  65. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  66. Hibernate: create table per_addr (address_id bigint not null, person_id bigint not null, primary key (person_id, address_id))

  67. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  68. Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)

  69. Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)
  70. 一对一

  71. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  72. Hibernate: create table per_addr (person_id bigint not null, address_id bigint, primary key (address_id))

  73. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  74. Hibernate: alter table per_addr add constraint UK_hdwakolgq6oelbfuallvfbcn4 unique (person_id)

  75. Hibernate: alter table per_addr add index FK_hdwakolgq6oelbfuallvfbcn4 (person_id), add constraint FK_hdwakolgq6oelbfuallvfbcn4 foreign key (person_id) references person (person_id)

  76. Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)
  77. 多对多

  78. Hibernate: create table address (address_id bigint not null, primary key (address_id))

  79. Hibernate: create table per_addr (person_id bigint not null, address_id bigint not null, primary key (person_id, address_id))

  80. Hibernate: create table person (person_id bigint not null, primary key (person_id))

  81. Hibernate: alter table per_addr add index FK_8v3twe5k7nlb8wcjqvcpydab6 (address_id), add constraint FK_8v3twe5k7nlb8wcjqvcpydab6 foreign key (address_id) references address (address_id)

  82. 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)的更多相关文章

  1. web进修之—Hibernate 继承映射(5)

    先看三个类的继承关系,Payment是父类,CashPayment和CreditCardPayment是Payment的子类:   view plaincopy to clipboardprint p ...

  2. Hibernate学习笔记-Hibernate关系映射

    1. 初识Hibernate——关系映射 http://blog.csdn.net/laner0515/article/details/12905711 2. Hibernate 笔记8 关系映射1( ...

  3. web进修之—Hibernate起步(1)(2)

    想开始写博客了,尝试了CSDN和cnblog之后还是觉得cnblog更加简洁.专注(不过cnblog不支持搬家),所以把刚刚写的两篇学习博客链接放在这儿,这样这个系列也算是完整了: web进修之—Hi ...

  4. 【SSH 基础】浅谈Hibernate关系映射(4)

    继上篇博客 多对多关联映射(单向) 多对多对象关系映射,须要增加一张新表完毕基本映射. Hibernate会自己主动生成中间表 Hibernate使用many-to-many标签来表示多对多的关联,多 ...

  5. Hibernate关系映射时出现的问题

    在学习Hibernate框架的关系映射时,遇到了一个问题: INFO: HHH000422: Disabling contextual LOB creation as connection was n ...

  6. Hibernate关系映射之many-to-many

    1.建表 2.创建实体类及映射文件 Student.java类 public class Student implements java.io.Serializable { // Fields pri ...

  7. hibernate关系映射

    多对一:比如多个订单对应同一个用户,需要在订单表中添加一个用户的属性 订单类: private Integer orderId; private Date createTime; private Us ...

  8. Hibernate关系映射(注解)

    1.类级别注解 @Entity     映射实体类 @Table    映射数句库表 @Entity(name="tableName") - 必须,注解将一个类声明为一个实体bea ...

  9. Hibernate关系映射(三) 多对多

    一.使用用户User和Role实现多对多的示例 User.java,实现对Role的引用 package com.lxit.entity; import java.util.HashSet; impo ...

随机推荐

  1. vue插件官方文档,做个记录

    vue的插件,组件都可以按照这种方式添加 官方文档 https://cn.vuejs.org/v2/guide/plugins.html 做个记录用

  2. appium定位

    一.链接基本信息 二.在appium界面中 三,定位 三.通过ui automator viewer抓取手机页面元素,点击红框按钮会抓取当前手机界面app全部元素;路径在sdk>tools下面的 ...

  3. 19南昌网络赛L

    校赛打杂没施展开. 题意:一开始给你一颗 (0,0)到(0,l)的树. 这棵树每一年会长出来三个幼芽(雾),长度均为l/4,方向分别是左转60,右转60,和不变. 年份<=14 考虑3^14直接 ...

  4. Spring Cloud 组件 —— eureka

    官方文档,Spring Cloud 对其封装,Spring Cloud eureka 文档

  5. 严重: A child container failed during start

    四月 20, 2019 4:54:28 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler [&qu ...

  6. mybatis3源码阅读之SqlSessionFactoryBuilder

    /** 构造器,根据配置或者代码生成SqlSessionFactory,采用分布构建的Builder模式 /* public class SqlSessionFactoryBuilder { /** ...

  7. linux 使用sh@d0ws0cks client

    Linux Centos7下安装使用Shadowsocks客户端,实现*** 准备 SS: 搭建一个可以连接外网的服务器 教程可见 自己动手搭梯子 服务器:本人用的腾讯云服务器,系统为Centos7 ...

  8. mysql查看编码格式以及修改编码格式

    1.进入mysql,输入show variables like 'character%';查看当前字符集编码情况,显示如下: 其中,character_set_client为客户端编码方式: char ...

  9. OSGi类加载流程

    思路 OSGi每个模块都有自己独立的classpath.如何实现这一点呢?是因为OSGi采取了不同的类加载机制: OSGi为每个bundle提供一个类加载器,该加载器能够看到bundle Jar文件内 ...

  10. [Swift]LeetCode876. 链表的中间结点 | Middle of the Linked List

    Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...