1) Establish the person class:
public class Person {private Integer id;private string name;private idcard idcard;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public Integer GetId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public Idcard Getidcard () {return idcard;} public void Setidcard (Idcard card) {this. Idcard = Card;} @Overridepublic String toString () {return "person [id=" + ID + ", name=" + name + "]";}}
2) Establish Idcard class:
public class Idcard {private Integer id;private String number;private person person;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetNumber () {return number;} public void Setnumber (String number) {this.number = number;} Public Person Getperson () {return person;} public void Setperson (person person) {This.person = person;} @Overridepublic String toString () {return "Idcard [id=" + ID + ", number=" + number + "]";}}
3) Create a persistence class:
public class Perandiddao {/** * Save method */@Testpublic void Testsave () {Session session = Sessionfactorytools.getsession (); T Ransaction tx = null;try {tx = Session.begintransaction ();//===============================================// New two Person Object person1 = new Man ();p erson1.setname ("Zhang San"); Person Person2 = new person ();p erson2.setname ("John Doe");//New two Idcard object Idcard idCard1 = new Idcard (); Idcard1.setnumber (" 37292501 "); Idcard idCard2 = new Idcard (); Idcard2.setnumber (" 37292502 ");//Add Associated object Person1.setidcard (IDCARD1); Person2.setidcard (IDCARD2); Idcard1.setperson (Person1); Idcard2.setperson (person2);//Save Session.save (Person1); Session.save (Person2); Session.save (idCard1); Session.save (IDCARD2);//========================================== =====tx.commit ();} catch (RuntimeException e) {if (tx! = null) {Tx.rollback ();} throw e;} finally {session.close ();}} /** * GetByID method */@Testpublic void Testgetbyid () {Session session = Sessionfactorytools.getsession (); Transaction tx = null;try {tx = Session.begiNtransaction ();//===============================================//Gets the person team elephant and prints information about the team elephant and associated idcard information person person = (person) session.get (Person.class, 1); SYSTEM.OUT.PRINTLN (person + ":"); System.out.println (Person.getidcard ());//Gets the Idcard object and prints the information of the change team image and the associated person information idcard Idcard = (idcard) session.get ( Idcard.class, 2); System.out.println (Idcard + ":"); System.out.println (Idcard.getperson ());//===============================================tx.commit ();} catch (RuntimeException e) {if (tx! = null) {Tx.rollback ();} throw e;} finally {session.close ();}} /** * method to disassociate relationships */@Testpublic void Testremoverelation () {Session session = Sessionfactorytools.getsession (); Transaction tx = null;try {tx = Session.begintransaction ();//===============================================/** * Gets the person object and sets its associated idcard to NULL in many-to-one, * because a party does not have foreign key management rights. So releasing a foreign key association is not possible * person person = (person) session.get (Person.class, 1); * Person.setidcard (NULL); *//** * Gets the Idcard object and sets its associated person to NULL in many-to-one, * because the multi-party has administrative privileges on the foreign key, the cancellation of the foreign key is possible */idcard Idcard = (Idcard) session.get (Idcard.class, 1); Idcard.setperson (null);//========================================== =====tx.commit ();} catch (RuntimeException e) {if (tx! = null) {Tx.rollback ();} throw e;} finally {session.close ();}} /** * method to delete the team image */@Testpublic void Testdelete () {Session session = Sessionfactorytools.getsession (); Transaction tx = null;try {tx = Session.begintransaction ();//===============================================/** * Gets the person object and deletes the object * in many-to-one. Because one party does not have administrative privileges on the foreign key. So it is impossible to delete this object * person person = (person) session.get (Person.class, 1); * Session.delete (person); *//** * Gets the Idcard object and deletes the object * in many-to-one. Because the multi-party has the foreign key to love your administrative rights, * So delete this object is able to implement */idcard Idcard = (idcard) session.get (Idcard.class, 1); SESSION.D Elete (Idcard);//===============================================tx.commit ();} catch (RuntimeException e) {if (tx! = null) {Tx.rollback ();} throw e;} finally {session.close ();}}}
4) Configuration of Person.hbm.xml:
<?XML version= "1.0"? ><! DOCTYPE hibernate-mapping public "-//hibernate/hibernate mapping DTD 3.0//en"
5) Configuration of IdCard.hbm.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 ">
6) configuration of the master file:
<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-configuration-3.0.dtd ">
Hibernate_8_person and Idcard Instances _ one-to-one relationships: based on foreign keys