Hibernate -- A unidirectional one-to-one association on a foreign key
2014-01-11 15:17
267 查看
atsometimeweusuallyneedtocreatetwotablesthatonetablerelateanother.Suchasahusbandonly
haveawife.SohowcanIdesignrelationshiplikethis.Inprogramming,Thetypeofthisrelationship
named“unidirectionalone-to-oneassociation”.
Howtoimplementthisrelationshipwithhibernate?
exampleforhusbandandwife.
Whencreateobjecthusbandandwife,youcanrelatetwoobjectlikethis:
TheHasbandobject:
[code]publicclassHusband{
[/code]
TheWifeobject:
[code]publicclassWife{
[/code]
Weuseannotationconfigurationhibernatebydefault.Soyousawtheannotitionlike@Entityincode.
WifeandHusbandbothareused@Entity.Itmeansthatobjectwillmappingtodatabase.
ThegetId()methodinWifeandHusbandbothareused@Idand@GeneratedValue
ThegetWife()methodinHusbandobjecthasannotationlikethis:
@OneToOne
@JoinColumn(name="wifeId")
Then,thecolumnwhichisnamedwifeIdwillebecreated.
So,Useannotationissosimple.that;sall.
DofromthatwecangetcreatetableSQLlikethis:
[code]idintegernotnullauto_increment,
[/code]
Finally,Talkaboutxmlconfigure.Whenusexmltoconfigurehibernatemoretroublethanuseannotations:
Youneedwritelikethisinxmlfile:
<many-to-onename="wife"column="wifId"unique="true"></many-to-one>
haveawife.SohowcanIdesignrelationshiplikethis.Inprogramming,Thetypeofthisrelationship
named“unidirectionalone-to-oneassociation”.
Howtoimplementthisrelationshipwithhibernate?
exampleforhusbandandwife.
Whencreateobjecthusbandandwife,youcanrelatetwoobjectlikethis:
TheHasbandobject:
@Entity
[code]publicclassHusband{
privateintid;
privateStringname;
privateWifewife;
@Id
@GeneratedValue
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
@OneToOne
@JoinColumn(name="wifeId")
publicWifegetWife(){
returnwife;
}
publicvoidsetWife(Wifewife){
this.wife=wife;
}
}
[/code]
TheWifeobject:
@Entity
[code]publicclassWife{
privateintid;
privateStringname;
@Id
@GeneratedValue
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
}
[/code]
Weuseannotationconfigurationhibernatebydefault.Soyousawtheannotitionlike@Entityincode.
WifeandHusbandbothareused@Entity.Itmeansthatobjectwillmappingtodatabase.
ThegetId()methodinWifeandHusbandbothareused@Idand@GeneratedValue
ThegetWife()methodinHusbandobjecthasannotationlikethis:
@OneToOne
@JoinColumn(name="wifeId")
Then,thecolumnwhichisnamedwifeIdwillebecreated.
So,Useannotationissosimple.that;sall.
DofromthatwecangetcreatetableSQLlikethis:
createtableHusband(
[code]idintegernotnullauto_increment,
namevarchar(255),
wifeIdinteger,
primarykey(id)
)
2014-1-1114:46:41org.hibernate.tool.hbm2ddl.SchemaExportperform
createtableWife(
idintegernotnullauto_increment,
namevarchar(255),
primarykey(id)
)
altertableHusband
addconstraintFK_kruq9jfxa0jrc2od8dbh09mia
foreignkey(wifeId)
referencesWife(id)
[/code]
Finally,Talkaboutxmlconfigure.Whenusexmltoconfigurehibernatemoretroublethanuseannotations:
Youneedwritelikethisinxmlfile:
<many-to-onename="wife"column="wifId"unique="true"></many-to-one>
相关文章推荐
- Hibernate映射关系解析(二)--Unidirectional associations--one-to-one
- Hibernate单向一对多关联(Unidirectional One-To-Many)要点
- Hibernate映射关系解析(三)--Unidirectional associations--one-to-many
- Change Bidirectional Association to Unidirectional(将双向关联改为单向)
- 重新组织数据之七 :Change Unidirectional Association to Bidirectional(将单向关联改为双向)
- hibernate映射文件set key one-to-many
- why inverse must be setted in hibernate bidirectional association of one-to-many and many-to-many
- Change Bidirectional Association to Unidirectional
- Hibernate OneToOne mapping primarykey [xml]
- Hibernate学习笔记(3)Hibernate映射文件之set key one-to-many
- Hibernate OneToOne mapping primary key [Annotation]
- [Linux] Enable ssh key authentication from one user on client to multiple users on server
- Change Unidirectional Association to Bidirectional
- 重构手法25:Change Bidirectional Association to Unidirectional (将双向关联改为单向关联)
- 8.8 change bidirectional association to unidirectional (将双向关联改为单向关联)
- org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.demo.pojo.IdCard
- Hibernate映射关系解析(一)--Unidirectional associations--Many-to-one
- 重构的小故事 Change Bidirectional Association to Unidirectional or Vice Verse
- Change Unidirectional Association to Bidirectional(将单向关联改为双向)
- 重构手法24:Change Unidirectional Association to Bidirectional (将单向关联改为双向关联)