Hibernate 一对一主键双向关联

一对一主键映射在一对一映射中还算是最为常用的。
 
一、模型
 
一个人Person 对应一个地址Address。
 
二、数据模型和对象模型图
 
Hibernate 一对一主键双向关联
 
导出建表SQL如下:
 
/*==============================================================*/

/*DBMSname:MySQL5.0*/

/*Createdon:2008-12-823:05:32*/

/*==============================================================*/

droptableifexistsaddress;

droptableifexistsperson;

/*==============================================================*/

/*Table:address*/

/*==============================================================*/

createtableaddress

(

idbigintnotnullcomment'ID',

detailvarchar(120)notnullcomment'详细地址',

primarykey(id)

)

type=InnoDB;

altertableaddresscomment'地址';

/*==============================================================*/

/*Table:person*/

/*==============================================================*/

createtableperson

(

idbigintnotnullauto_incrementcomment'ID',

namevarchar(24)notnullcomment'姓名',

primarykey(id)

)

type=InnoDB;

altertablepersoncomment'人';

altertableaddressaddconstraintFK_Reference_2foreignkey(id)

            references person (id) on delete restrict on update restrict;
 
三、对象模型代码
 
public class Person implements java.io.Serializable {

privateLongid;

privateStringname;

  private Address address;
 
public class Address implements java.io.Serializable {

privateLongid;

privatePersonperson;

  private String detail;
 
四、映射代码
<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<classname="entity.Person"table="person">

<idname="id"type="java.lang.Long">

<columnname="id"/>

<generatorclass="identity"/>

</id>

<propertyname="name"type="java.lang.String">

<columnname="name"length="24"not-null="true">

<comment>姓名</comment>

</column>

</property>

<!--cascade="all":在保存person对象的时候,级联保存person对象关联的address对象-->

<one-to-onename="address"cascade="all"/>

</class>

</hibernate-mapping>
 
<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<classname="entity.Address"table="address"catalog="mydb">

<idname="id"type="java.lang.Long">

<columnname="id"/>

<!--class="foreign":一对一主键映射中,使用另外一个相关联的对象的标识符-->

<generatorclass="foreign">

<paramname="property">person</param>

</generator>

</id>

<propertyname="detail"type="java.lang.String">

<columnname="detail"length="120"not-null="true">

<comment>详细地址</comment>

</column>

</property>

<!--表示在address表存在一个外键约束,外键参考相关联的表person-->

<one-to-onename="person"constrained="true"/>

</class>

</hibernate-mapping>
 
五、Hibernate配置
<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!--GeneratedbyMyEclipseHibernateTools.-->

<hibernate-configuration>

<session-factory>

<propertyname="connection.username">root</property>

<propertyname="connection.url">

jdbc:mysql://localhost:3306/mydb

</property>

<propertyname="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<propertyname="connection.password">xiaohui</property>

<propertyname="connection.driver_class">

com.mysql.jdbc.Driver

</property>

<propertyname="show_sql">true</property>

<propertyname="format_sql">true</property>

<mappingresource="entity/Person.hbm.xml"/>

<mappingresource="entity/Address.hbm.xml"/>

</session-factory>

</hibernate-configuration>

相关推荐