java

位置:IT落伍者 >> java >> 浏览文章

Hibernate各种映射关系总结


发布日期:2024年04月16日
 
Hibernate各种映射关系总结

多对一

第一步 建立对象模型

User

idint

nameString

groupGroup

Group

id:int

name:String

第二步映射文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Usertable=t_user>

<idname=idcolumn=id>

<generatorclass=identity/>

</id>

<propertyname=namecolumn=User_nameunique=true/>

<manytoonename=groupcolumn=groupid/>

</class>

</hibernatemapping>

一对一映射

主键映射

建立对象模型

Person[idintnameString]

IdCard[idintcardNoint]

映射文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Persontable=t_person>

<idname=id>

<generatorclass=foreign>

<paramname=property>idCard</param>

</generator>

</id>

<propertyname=namecolumn=Person_name/>

<onetoonename=idCardconstrained=true/>

</class>

</hibernatemapping>

主键映射双向

只需要在IdCard 的XML中添加<onetoone>标签指向person就可以 告诉Hibernate如何加载person默认按照主键加载

外键关联映射

对象模型

Person[idintnameStringidcardIdCard]

IdCard[idintcardNoint]

mapping文件

Xml代码

Personxml

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Persontable=t_person>

<idname=id>

<generatorclass=identity>

</generator>

</id>

<propertyname=namecolumn=Person_name/>

<manytoonename=idCardunique=true></manyto one>

</class>

</hibernatemapping>

onetoone 外键双向mapping

只需要在另一端(IdCard)加入<onetoone>标签但是hibernate默 认按照主键加载所以要告诉hibernate按照指定列来加载

示意

Xml代码

Idcard

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=IdCardtable=t_idcard>

<idname=idcolumn=id>

<generatorclass=identity/>

</id>

<propertyname=cardNocolumn=cardno/>

<onetoonename=personpropertyref=idCard></oneto one>

</class>

</hibernatemapping>

一对多映射

建立对象模型

Classes[idintnameStringstudentSet]

Student[idintnameString]

映射文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Classtable=t_class>

<idname=id>

<generatorclass=native/>

</id>

<propertyname=name/>

<setname=student>

<keycolumn=classid/>

<onetomanyclass=Student/>

</set>

</class>

</hibernatemapping>

缺点若将Student的classid设置为非空将无法保存应为不是student这 一端维护关系所以student不知道是哪个班级所以需要发出多余的update语 句来更新关系

(一对多) 先要sessionsava(学生)再sessiosave(班级)

(多对一) 先要sessionsava(班级)再sessiosave(学生)

改进 采用 一对多双向关联

Xml代码

<setname=studentinverse=true>

<setname=studentinverse=true>

这样的好处是 存入数据让多的一端进行维护不需要发出多余的 update

多对多mapping

单向(采用第三方表)

对象模型

Role[idintnameString]

User[idintnameStringroleSet]

映射文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Usertable=t_user>

<idname=id>

<generatorclass=native/>

</id>

<propertyname=name/>

<setname=roletable=t_user_role>

<keycolumn=userid></key>

<manytomanyclass=Rolecolumn=roleid/>

</set>

</class>

</hibernatemapping>

双向

对象模型

Role[idintnameStringuserSet]

User[idintnameStringroleSet]

mapping文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Roletable=t_role>

<idname=id>

<generatorclass=native/>

</id>

<propertyname=name/>

<setname=usertable=t_user_role>

<keycolumn=userid></key>

<manytomanyclass=Usercolumn=userid/>

</set>

</class>

</hibernatemapping>

继承mapping

第一类继承映射

对象模型

Animal

privateintid

privateStringname

privateBooleansex

publicclassBirdextendsAnimal

privateintheight

publicclassPigextendsAnimal

privateintweight

mapping 文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Animaltable=t_animal>

<idname=id>

<generatorclass=native/>

</id>

<discriminatorcolumn=type type=string></discriminator>

<propertyname=name/>

<propertyname=sex/>

<subclassname=Pigdiscriminatorvalue=P>

<propertyname=weight/>

</subclass>

<subclassname=Birddiscriminatorvalue=B>

<propertyname=height/>

</subclass>

</class>

</hibernatemapping>

第二类mapping

对象模型

mapping 文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Animaltable=t_animal>

<idname=id>

<generatorclass=native/>

</id>

<propertyname=name/>

<propertyname=sex/>

<joinedsubclassname=Pigtable=t_pig>

<keycolumn=pid/>

<propertyname=weight/>

</joinedsubclass>

<joinedsubclassname=Birdtable=t_bird>

<keycolumn=bid/>

<propertyname=height/>

</joinedsubclass>

</class>

</hibernatemapping>

第三类mapping

对象模型

mapping 文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Animaltable=t_animal>

<idname=id>

<generatorclass=native/>

</id>

<propertyname=name/>

<propertyname=sex/>

<unionsubclassname=Pigtable=t_pig>

<propertyname=weight/>

</unionsubclass>

<unionsubclassname=Birdtable=t_bird>

<propertyname=height/>

</unionsubclass>

</class>

</hibernatemapping>

Component 映射

component不是实体没有OID

对象模型

publicclassEmployee

privateintid

privateStringname

privateContactcontact

publicclassContact

privateStringemail

privateStringaddress

privateStringzipcode

privateStringcontactTel

publicclassUser

privateintid

privateStringname

privateContactcontact

mapping文件

Xml代码

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=Usertable=t_user>

<idname=id>

<generatorclass=native/>

</id>

<propertyname=name/>

<componentname=contact>

<propertyname=email></property>

<propertyname=address></property>

<propertyname=zipcode></property>

<propertyname=contactTel></property>

</component>

</class>

</hibernatemapping>

复合主键mapping

通常将复合主键相关属性单独放到一个类中

这个类必须实现 Serializable接口

这个类必须重写hashCode()和equals()方法

XML

<?xmlversion=?>

<!DOCTYPEhibernatemappingPUBLIC

//Hibernate/HibernateMappingDTD//EN

hiber/hibernatemappingdtd>

<hibernatemappingpackage=comnintenyunhibernate>

<classname=SCtable=t_sc>

<compositeidname=scpk>

<keypropertyname=sno/>

<keypropertyname=cno/>

</compositeid>

<propertyname=sname></property>

<propertyname=cname></property>

</class>

</hibernatemapping>

               

上一篇:Hibernate session的方法的一些说明

下一篇:Eclipse平台扩展点清单