Account.hbm.xml<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.hibernate.entity.Account" table="account" catalog="hibernate">
<id name="accountId" type="java.lang.Long">
<column name="ACCOUNT_ID" precision="10" scale="0" />
<generator class="assigned" />
</id>
<property name="accountNumber" type="java.lang.String">
<column name="ACCOUNT_NUMBER" />
</property>
<!-- 注意使用完整类名 -->
<one-to-one name="user" class="com.hibernate.entity.User" property-ref="account"></one-to-one>
</class>
</hibernate-mapping>
User.hbm.xml
<?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.hibernate.entity.User" table="user" catalog="hibernate">
<id name="userId" type="java.lang.Long">
<column name="USER_ID" precision="10" scale="0" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" not-null="true" />
</property>
<!-- 共享主键方式映射 one to one 注意 class 要用完整类名-->
<one-to-one name="relAddress" class="com.hibernate.entity.Address" cascade="save-update"></one-to-one>
<!-- 单独列外键通过many to one + unique 属性方式映射 one to one 注意 class 要用完整类名-->
<many-to-one name="account" class="com.hibernate.entity.Account" column="USER_ACCOUNT_ID" unique="true" cascade="save-update"></many-to-one>
</class>
</hibernate-mapping>
Address.hbm.xml
<?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.hibernate.entity.Address" table="address" catalog="hibernate">
<id name="addressId" type="java.lang.Long">
<column name="ADDRESS_ID" />
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="street" type="java.lang.String">
<column name="STREET" not-null="true" />
</property>
<!-- 映射 one to one 注意 class 要用完整类名-->
<one-to-one name="user" class="com.hibernate.entity.User" constrained="true" ></one-to-one>
</class>
</hibernate-mapping>
工程文件已经打包,包含数据库生成.SQL
本文详细解析了使用MyEclipse Persistence Tools生成的数据库映射配置XML文件,包括Account、User、Address实体的映射规则及主键关联方式。
1635

被折叠的 条评论
为什么被折叠?



