Jpa bidirectional one-to-one Association

Source: Internet
Author: User

Jpa bidirectional one-to-one Association
1.1. Relationship maintainer

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 association configuration * If mappedBy is not configured, optional is configured on the Link maintenance end *, the foreign key cannot be blank, the other side cannot be empty. You do not need to configure */@ OneToOne (cascade = CascadeType. 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. Link maintainer

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;/*** mappedBy = "idCard" is specified as the link maintainer */@ OneToOne (cascade = {CascadeType. PERSIST, CascadeType. MERGE}, fetch = FetchType. EAGER, mappedBy = "idCard") private Person 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;} public Person getPerson () {return person;} public void setPerson (Person person) {this. person = person ;}}


1.3. Test

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.createEntityManagerFactory("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();          }    } 


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.