Hibernate各种映射关系总结
时间:2010-12-14 javaeye nintenyun
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 一对一映射
Hibernate?÷??ó3é?1??μ×ü?á(2)
ê±??:2010-12-14 javaeye nintenyun
2.1 ?÷?üó3é?
1. ?¨á¢???ó?£Dí
Person[id-int,name-String]
idcard[id-int,cardNo-int]
2. ó3é????t
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>
2.2 ?÷?üó3é????ò
??Dèòa?úIdCard μ?XML?Dìí?ó<one-to-one>±ê?????òperson£??í?éò? ????Hibernateè?o??ó??person£???è?°′???÷?ü?ó??
2.3 ía?ü1?áaó3é?
1. ???ó?£Dí
Person[id-int£?name-String£?idcard-IdCard]
IdCard[id-int£?cardNo-int]
2 mapping???t
Xml′ú??
Person.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="identity">
???????? </generator>
?????? </id>
?????? <property??name="name"??column="Person_name"??/>
?????? <many-to-one??name="idCard"??unique="true"></many-to- one>
???? </class>
</hibernate-mappi
|