One-to-one association ing (primary key + bidirectional)

Source: Internet
Author: User





One-to-one association ing (bidirectional) To implement one-to-one bidirectional Association ing, let's take the person and ID card as an example to understand:

Hibernate one-to-one primary key Association ing (two-way Association person <----> idcard) needs to add the <one-to-one> tag to the idcard ing file to point to person, it instructs hibernate how to load person based on primary key by default.


Step 1: first, establish the object relation class of the person and ID card:

Public class idcard { Private int ID; Private string cardno; Private person; // you must also hold an idcord reference in person. Public int GETID (){ Return ID; } Public void setid (int id ){ This. ID = ID; } Public String getcardno (){ Return cardno; } Public void setcardno (string cardno ){ This. cardno = cardno; } Public Person getperson (){ Return person; } Public void setperson (person ){ This. Person = person; }}

This is the object link class of person:

Public class person { Private int ID; Private string name; Private idcard; // you must also hold an idcord reference in person. 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 idcard getidcard (){ Return idcard; } Public void setidcard (idcard ){ This. idcard = idcard; } }


Step 2: Create an object ing file:The person ing file of the person object is:

<Hibernate-mapping> <Class name = "com. bjsxt. hibernate. Person" table = "t_person"> <ID name = "ID"> <Generator class = "foreign"> <Param name = "property"> idcard </param> </Generator> </ID> <Property name = "name"/> <One-to-one name = "idcard" constrained = "true"/> </Class> <Class name = "com. bjsxt. hibernate. idcard" table = "t_idcard">
<ID name = "ID">
<Generator class = "native"/>
</ID>
<Property name = "cardno"/>
<One-to-one name = "person"/> // Add <one-to-one name = "person"/> to the idcord ing File
</Class>
</Hibernate-mapping> Step 3: add the hibernate configuration file: <Session-factory>
<Property name = "hibernate. Connection. url"> JDBC: mysql: // localhost/hibernate_one2one_pk_2 </property>
<Property name = "hibernate. Connection. driver_class"> com. MySQL. JDBC. Driver </property>
<Property name = "hibernate. Connection. username"> root </property>
<Property name = "hibernate. Connection. Password"> bjsxt </property>
<Property name = "hibernate. dialect"> org. hibernate. dialect. mysqldialect </property>
<Property name = "hibernate. show_ SQL"> true </property>
<Mapping Resource = "com/bjsxt/hibernate/idcard. HBM. xml"/>
<Mapping Resource = "com/bjsxt/hibernate/person. HBM. xml"/>
</Session-factory>
</Hibernate-configuration> Step 4: Write Test Code :
Public class one2onetest extends testcase {
Public void testsave1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
Idcard = new idcard ();
Idcard. setcardno ("88888888888888 ");
Person = new person ();
Person. setname ("dish 10 ");
Person. setidcard (idcard );
// Transientobjectexception does not occur
// Because the cascade attribute is default in one-to-one primary key Association ing
Session. Save (person );
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testload1 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
Person = (person) Session. Load (person. Class, 1 );
System. Out. println ("person. Name =" + person. getname ());
System. Out. println ("idcard. cardno =" + person. getidcard (). getcardno ());
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
Public void testload2 (){
Session session = NULL;
Try {
Session = hibernateutils. getsession ();
Session. begintransaction ();
// Add from the idcord end
Idcard = (idcard) Session. Load (idcard. Class, 1 );
System. Out. println ("idcard. cardno =" + idcard. getcardno ());
System. Out. println ("idcard. Person. Name =" + idcard. getperson (). getname ());
Session. gettransaction (). Commit ();
} Catch (exception e ){
E. printstacktrace ();
Session. gettransaction (). rollback ();
} Finally {
Hibernateutils. closesession (session );
}
}
}





































































































Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.