首页 > 代码库 > jpa双向一对一关联关系

jpa双向一对一关联关系

1.1.  关系维护方

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;
   
    /**
     * 一对一关联关系配置
     * 没有配置mappedBy即为关系维护端
     * 配置了optional说明外键不能为空,在另一方也同样不能为空,不需要再配置了
     */
    @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.  关系被维护方

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" 为关系被维护方
     */
    @OneToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.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;
    }
 
    public Person getPerson() {
       return person;
    }
 
    public void setPerson(Person person) {
       this.person = person;
    }
 
}


1.3.  测试

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


 

 

jpa双向一对一关联关系