- 背景:
一个部门只有一个一把手,这在程序开发中就会设计数据映射应该设置为一对一关联。
在hibernate代码开发中,实现这个业务有两种方案:
1)基于外键映射的1-1关联;
2)基于主键映射的1-1关联。
本篇文章主要是用来学习如何使用外键实现1-1关联关系。
- 新建项目hibernate05
新建java project,引入依赖包,在src下添加hibernate.cfg.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="hibernate.connection.username">root</property> 8 <property name="hibernate.connection.password">123456</property> 9 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_01</property> 11 12 <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 13 <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> --> 14 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 15 16 <property name="hibernate.show_sql">true</property> 17 18 <property name="hibernate.format_sql">true</property> 19 20 <property name="hibernate.hbm2ddl.auto">update</property> 21 22 <property name="hibernate.current_session_context_class">thread</property> 23 24 <property name="hibernate.c3p0.max_size">500</property> 25 <property name="hibernate.c3p0.min_size">20</property> 26 <property name="hibernate.c3p0.max_statements">10</property> 27 <property name="hibernate.c3p0.timeout">2000</property> 28 <property name="hibernate.c3p0.idle_test_period">2000</property> 29 <property name="hibernate.c3p0.acquire_increment">10</property> 30 31 <mapping resource="com/dx/hibernate005/onetoonebyforigenkey/Deparment.hbm.xml" /> 32 <mapping class="com.dx.hibernate005.onetoonebyforigenkey.Deparment" /> 33 <mapping resource="com/dx/hibernate005/onetoonebyforigenkey/Manager.hbm.xml" /> 34 <mapping class="com.dx.hibernate005.onetoonebyforigenkey.Manager" /> 35 </session-factory> 36 </hibernate-configuration>