Hibernate 一对一主键双向关联
/*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;privateLongid;
privateStringname;
private Address address;privateLongid;
privatePersonperson;
private String detail;<!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><!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><!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>