与上篇相对应,还是client和address是一对一的外键关联关系,address表中的主键ID与client表中的外键CLIENTADDRESSID构成一对一外键关联关系.
表结构和sql语句就不说了,看下外键双向关联和外键单向关联的不同点和注意项:
双向关联:
可以根据client得到address,也可以根据address得到client,双向关联时需要在client.hbm.xml中配置
<many-to-one unique=”true” name=”client_address” column=”CLIENTADRESSID” cascade=”all”></many-to-one>;
<one-to-one name=”address_client” property-ref=”client_address”></one-to-one>,property-ref的值应与<many-to-one>中的name属性的值对应,说明client对象是与address对象建立的关联关系.
只可以根据client得到address,单向关联时只需在client.hbm.xml中配置
<many-to-one unique=”true” name=”client_address” column=”CLIENTADDRESSID” cascade=”all”>
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <property name="connection.url">jdbc:mysql://localhost:3306/onetoone</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 显示sql语句 --> <property name="hibernate.show_sql">true </property> <property name="format_sql">true</property><!-- 让输出的sql语句格式化 --> <mapping resource="com/hust/javabeans/Address.hbm.xml" /> <mapping resource="com/hust/javabeans/Client.hbm.xml" /> </session-factory> </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping> <class name="com.hust.javabeans.Client" table="client"> <id name="id" column="ID" type="integer"> <generator class="identity"></generator> </id> <property name="clientname" column="CLIENTNAME" type="string"></property> <property name="phone" column="PHONE" type="string"></property> <property name="email" column="EMAIL" type="string"></property> <!-- 映射Client和Address的一对一外键关联,唯一多对一,实际上时一对一关系,这里的name是Client的属性,column是外键--> <many-to-one name="client_address" class="com.hust.javabeans.Address" column="CLIENTADDRESSID" cascade="all" unique="true"></many-to-one> </class> </hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.hust.javabeans.Address" table="address"> <id column="ID" name="id" type="integer"> <generator class="identity"/> </id> <property name="province" column="PROVINCE" type="string"/> <property name="city" column="CITY" type="string"/> <property name="street" column="STREET" type="string"/> <property name="zipcode" column="ZIPCODE" type="string"/> <!-- 映射Client和Address的一对一外键关联,name是Address的属性,property-ref是Client中的属性client_address,many-to-one的name属性的值,表明建立了从address对象到company对象的关联 --> <!-- <one-to-one name="address_client" class="com.hust.javabeans.Client" property-ref="client_address"/> --> </class> </hibernate-mapping>