Hibernate_8_Person and IdCard instance _ one-to-one relationship: Based on Foreign keys
1) create a 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) create an 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) Establish a persistence class:
Public class perAndIdDao {/*** save Method */@ Testpublic void testSave () {Session session = SessionFactoryTools. getSession (); Transaction tx = null; try {tx = session. beginTransaction (); // ================================================ ============/// create two Person objects Person person1 = new Person (); person1.setName ("Zhang San"); Person person2 = new Person (); person2.setName ("Li Si"); // create two IdCard objects: IdCard idCard1 = new IdCard (); idCard1. SetNumber ("37292501"); IdCard idCard2 = new IdCard (); idCard2.setNumber ("37292502"); // Add the correlated object person1.setIdCard (idCard1); person2.setIdCard (idCard2 ); idCard1.setPerson (person1); idCard2.setPerson (person2); // Save the 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 (); // ================================================ ============/// obtain the person team image and print the team image information and the associated idCard information Person person = (Person) session. get (Person. class, 1); System. out. println (person + ":"); System. o Ut. println (person. getIdCard (); // obtain the idCard object and print the team image information 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 for disassociation */@ Testpublic void testRemoveRelation () {Session session = SessionFactoryTools. getSession (); Transaction tx = null; try {tx = session. beginTransaction (); // ================================================ ===========/*** get the person object and set its associated IdCard to null in many-to-one, * because one party does not have foreign key management rights, disassociation of Foreign keys cannot be implemented in * Person person = (Person) session. get (Person. class, 1); * person. setIdCard (null); * // *** get the idCard object and set its associated person to null in many-to-one. * because multiple parties have foreign key management permissions, therefore, the release of this foreign key can be implemented */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 for deleting a queue image */@ Testpublic void testDelete () {Session session = SessionFactoryTools. getSession (); Transaction tx = null; try {tx = session. beginTransaction (); // ================================================ ===========/ *** get the person object and delete it * in many-to-one, because one party does not have the management permission of the foreign key, it is impossible to delete this object * Person person = (Person) session. get (Person. class, 1); * session. dele Te (person); * // *** get the idCard object and delete it. * In many-to-one mode, multiple parties have the permission to manage the foreign key and love you, * This object can be deleted */IdCard idCard = (IdCard) session. get (IdCard. class, 1); session. delete (idCard ); // ================================================ ========= tx. commit ();} catch (RuntimeException e) {if (tx! = Null) {tx. rollback () ;}throw e ;}finally {session. close ();}}}
4) Person. hbm. xml configuration:
5) IdCard. hbm. xml configuration:
6) configuration of the master file:
Org. hibernate. dialect. MySQLDialect
Jdbc: mysql: // hibernate0
Com. mysql. jdbc. Driver
Root
Root
False
False
Update