In hibernate, one-to-one association can be performed in two ways: foreign key Association and primary key Association.
One-to-one primary key bidirectional Association is used in recent projects and is based on annotations. I encountered some problems during this period. I will post them for further discussion.
A husband (husband) corresponds to a wife (wife). The main goal is to store the associated objects when the husband or wife is stored. The Code is as follows:
Husband class:
Package cn.edu. dlnu. Resources. model. entity;
Import javax. Persistence. cascadetype;
Import javax. Persistence. entity;
Import javax. Persistence. generatedvalue;
Import javax. Persistence. ID;
Import javax. Persistence. onetoone;
Import javax. Persistence. primarykeyjoincolumn;
@ Entity
Public class husband {
Private int ID;
Private string name;
Private wife;
@ ID
@ Generatedvalue // primary key generator
Public int GETID (){
Return ID;
}
Public String getname (){
Return name;
}
@ Onetoone (cascade = cascadetype. All)
@ Primarykeyjoincolumn // This annotation can only be written at one end of the master (generate ID)
Public wife getwife (){
Return wife;
}
Public void setid (int id ){
This. ID = ID;
}
Public void setname (string name ){
This. Name = Name;
}
Public void setwife (wife ){
This. Wife = wife;
}
}
Wife class:
Package cn.edu. dlnu. Resources. model. entity;
Import javax. Persistence. cascadetype;
Import javax. Persistence. entity;
Import javax. Persistence. generatedvalue;
Import javax. Persistence. ID;
Import javax. Persistence. onetoone;
Import org. hibernate. Annotations. genericgenerator;
Import org. hibernate. Annotations. parameter;
@ Entity
Public class wife {
Private int ID;
Private string name;
Private husb and;
@ ID
@ Genericgenerator (name = "pkgenerator", strategy = "foreign", parameters = {@ parameter (name = "property", value = "husband ")})
@ Generatedvalue (generator = "pkgenerator ")
// The wife ID is assigned based on the husband ID. The ID generator policy foreign needs to be set here. The parameter specifies that the wife ID uses the ID in the husband object.
Public int GETID (){
Return ID;
}
Public void setid (int id ){
This. ID = ID;
}
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public void sethusband (Husband ){
This. Husband = husband;
}
@ Onetoone (cascade = cascadetype. All, mappedby = "wife ")
Public husb and gethusb and (){
Return husband;
}
}
Onetoonepktest class:
Package cn.edu. dlnu. Resources. model. entity;
Import org. hibernate. Session;
Import org. hibernate. sessionfactory;
Import org. hibernate. cfg. annotationconfiguration;
Import org. hibernate. tool. hbm2ddl. schemaexport;
Import org. JUnit. afterclass;
Import org. JUnit. beforeclass;
Import org. JUnit. test;
Import cn.edu. dlnu. Resources. model. entity. husband;
Import cn.edu. dlnu. Resources. model. entity. wife;
Public class onetoonepktest {
Private Static sessionfactory;
@ Beforeclass
Public static void beforeclass (){
Sessionfactory = new annotationconfiguration (). Configure (). buildsessionfactory ();
}
@ Afterclass
Public static void afterclass (){
Sessionfactory. Close ();
}
@ Test
Public void testsave (){
Session S = sessionfactory. getcurrentsession ();
S. begintransaction ();
Husband H = new husband ();
Wife W = new wife ();
W. setname ("W ");
H. setname ("H ");
H. setwife (w );
W. sethusband (h );
S. Save (w );
S. gettransaction (). Commit ();
}
@ Test
Public void testschemaexport (){
New schemaexport (New annotationconfiguration (). Configure (). Create (false, true );
}
Public static void main (string [] ARGs ){
Beforeclass ();
New onetoonepktest (). testsave ();
Afterclass ();
}
}
Database Table generated by hibernate: Create Table husband (
Id integer not null auto_increment,
Name varchar (255 ),
Primary Key (ID)
);
Create Table wife (
Id integer not null,
Name varchar (255 ),
Primary Key (ID)
)
Run testsave (). After the method is run, husband in the database is consistent with the primary key recorded in the wife table.