Hibernate各种映射关系总结
2009-10-30 00:00:00 来源:WEB开发网1 多对一
第一步 :建立对象模型
User
-----------------------------
id:int
name:String
group:Group
Group
------------------------------
id:int
name:String
第二步:映射文件
Xml代码
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
<class name="User" table="t_user">
<id name="id" column="id">
<generator class="identity"/>
</id>
<property name="name" column="User_name" unique="true"/>
<many-to-one name="group" column="groupid"/>
</class>
</hibernate-mapping>
2 一对一映射
2.1 主键映射
1. 建立对象模型
Person[id-int,name-String]
IdCard[id-int,cardNo-int]
2. 映射文件
Xml代码
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.nintenyun.hibernate">
<class name="Person" table="t_person">
<id name="id">
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<property name="name" column="Person_name" />
<one-to-one name="idCard" constrained="true"/>
</class>
</hibernate-mapping>
赞助商链接