首页 > 代码库 > JPA学习---第十节:JPA中的一对一双向关联

JPA学习---第十节:JPA中的一对一双向关联

1、创建实体类,代码如下:

代码清单1:

package learn.jpa.entity;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.OneToOne;@Entitypublic class Person {    private int id;    private String name;    private IDCard idcard;        public Person(){            }        public Person(String name){        this.name = name;    }        @OneToOne(optional=false,cascade=CascadeType.ALL)    @JoinColumn(name="idcard_id")    public IDCard getIdcard() {        return idcard;    }    public void setIdcard(IDCard idcard) {        this.idcard = idcard;    }    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }        @Column(length=10,nullable=false)    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

代码清单2:

package learn.jpa.entity;import javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToOne;@Entitypublic class IDCard {    private int id;    private String cardno;    private Person person;        public IDCard(){            }        public IDCard(String cardno){        this.cardno = cardno;    }        @OneToOne(mappedBy="idcard",cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REFRESH},            optional=false)    public Person getPerson() {        return person;    }    public void setPerson(Person person) {        this.person = person;    }    @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }        @Column(length=18, nullable=false)    public String getCardno() {        return cardno;    }    public void setCardno(String cardno) {        this.cardno = cardno;    }    }

2、测试代码:

package learn.jpa.test;import static org.junit.Assert.*;import javax.persistence.EntityManager;import javax.persistence.EntityManagerFactory;import javax.persistence.Persistence;import learn.jpa.entity.IDCard;import learn.jpa.entity.Person;import org.junit.Test;public class OneToOneTest {    /**     * 测试数据库是否可以生成表     */    @Test    public void test() {        EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa");        factory.close();    }    @Test    public void save(){        EntityManagerFactory factory = Persistence.createEntityManagerFactory("learn_jpa");        EntityManager em = factory.createEntityManager();        em.getTransaction().begin();   // 开启事务                Person person = new Person("hwl");        IDCard idcard = new IDCard("435034189809170123");        idcard.setPerson(person);        person.setIdcard(idcard);        em.persist(person);        em.getTransaction().commit();        em.close();        factory.close();    }}

JPA学习---第十节:JPA中的一对一双向关联