JPA bidirectional one-to-one correlation relationship

Source: Internet
Author: User

1.1. Relationship Maintenance Party

Person.java

Package com.morris.entity; Import Javax.persistence.cascadetype;import Javax.persistence.column;import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.generatedvalue;import Javax.persistence.id;import Javax.persistence.joincolumn;import Javax.persistence.onetoone;import javax.persistence.Table;       @Entity @table (name= "persons") public class Person {@Id @GeneratedValue private Integer personId;       @Column (name= "Person_name", Length=50,nullable=false) private String personname; /** * One-to-one affinity configuration * No configuration Mappedby is configured for the relationship maintenance side * Optional Description foreign key cannot be empty, also cannot be empty on the other side, do not need to configure the */@OneToOne (CASCADE=CA     Scadetype.all,fetch=fetchtype.eager,optional=false) @JoinColumn (name= "card_id") private Idcard Idcard;    Public Integer Getpersonid () {return personId;    } public void Setpersonid (Integer personId) {This.personid = PersonId;    } public String Getpersonname () {return personname; } public void SETpersonname (String personname) {this.personname = PersonName;    } public Idcard Getidcard () {return idcard;    } public void Setidcard (Idcard idcard) {this.idcard = Idcard;       } Public person (Integer personId, String personname, Idcard Idcard) {super ();       This.personid = personId;       This.personname = PersonName;    This.idcard = Idcard;       } public person () {super (); TODO auto-generated Constructor stub} @Override public String toString () {return ' person [personid= ']    + PersonId + ", personname=" + PersonName + ", idcard=" + Idcard + "]";       } Public person (String PersonName) {super ();    This.personname = PersonName; }}


1.2. Relationship maintained by the party

Idcard.java

Package com.morris.entity; Import Javax.persistence.cascadetype;import Javax.persistence.column;import Javax.persistence.entity;import Javax.persistence.fetchtype;import Javax.persistence.generatedvalue;import Javax.persistence.id;import Javax.persistence.onetoone;import javax.persistence.Table;       @Entity @table (name= "Id_card") public class Idcard {@Id @GeneratedValue private Integer cardId;       @Column (name= "Card_no", Length=18,nullable=false) private String Cardno; /** * Specifies that mappedby= "Idcard" is the relationship-maintained party/@OneToOne (Cascade={cascadetype.persist,cascadetype.merge},fetch=fetchtyp     E.eager,mappedby= "Idcard") private person person;       Public Idcard (String Cardno) {super ();    This.cardno = Cardno;       } public Idcard () {super ();       TODO auto-generated Constructor stub} public Idcard (String Cardno, person person) {super ();       This.cardno = Cardno;    This.person = person; } @Override Public String TostriNg () {return "Idcard [cardid=" + CardId + ", cardno=" + Cardno + ", person=" + person + "]";    } public Integer Getcardid () {return cardId;    } public void Setcardid (Integer cardId) {this.cardid = CardId;    } public String Getcardno () {return cardno;    } public void Setcardno (String cardno) {this.cardno = Cardno;    Getperson () {return person;    } public void Setperson (person person) {This.person = person; } }


1.3. Testing

One2onetest.java

Package com.morris.test; Import Javax.persistence.entitymanager;import Javax.persistence.entitymanagerfactory;import Javax.persistence.Persistence; Import Org.junit.afterclass;import Org.junit.beforeclass;import org.junit.Test; Import Com.morris.entity.IDCard; Import Com.morris.entity.Person;       public class One2onetest {private static entitymanagerfactory factory;     private static Entitymanager manager; @BeforeClass public static void Setupbeforeclass () throws Exception {factory = Persistence.createentityman             Agerfactory ("Jpademo");             Manager = Factory.createentitymanager ();    Manager.gettransaction (). Begin ();             @Test public void Test () {Person person = new Person ("Morris");             Idcard card = new Idcard ("123456789");             Person.setidcard (card);          Manager.persist (person); } @AfterClass public static void Setupafterclass () throws Exception {manager.gettransaction (). cOmmit ();             Manager.close ();          Factory.close ();  }    }


JPA bidirectional one-to-one correlation relationship

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.