码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-11-11 21:05:16      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   java   sp   数据   

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;

@Entity
public 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;

@Entity
public 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中的一对一双向关联

标签:style   blog   io   color   ar   os   java   sp   数据   

原文地址:http://www.cnblogs.com/hwlsniper/p/4090420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!