码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate-----一对一唯一外键关联映射(注解)

时间:2014-11-29 13:18:18      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:唯一外键   hibernate   注解   一对一关联映射   

双向关联映射:

One(‘Many’)

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Person implements java.io.Serializable {
    private Integer id;
    private String name;
 
    private IdCard idCard;
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
       return id;
    }
 
    public void setId(Integer id) {
       this.id = id;
    }
 
    @Column(name = "person_name")
    public String getName() {
       return name;
    }
 
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "idCard_id")
    public void setName(String name) {
        this.name = name;
    }
 
    public IdCard getIdCard() {
       return idCard;
    }
 
    public void setIdCard(IdCard idCard) {
       this.idCard = idCard;
    }
}

One(‘One’)

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class IdCard implements java.io.Serializable {
    private Integer id;
    private String name;
 
    private Person person;
 
    @Id
    @GeneratedValue(generator = "TeacherGenerator")
    @GenericGenerator(name = "TeacherGenerator", strategy = "native")
    public Integer getId() {
       return id;
    }
 
    public void setId(Integer id) {
       this.id = id;
    }
 
    @Column(name = "name", nullable = false, length = 32)
    public String getName() {
       return name;
    }
 
    public void setName(String name) {
       this.name = name;
    }
 
    @OneToOne(mappedBy = "idCard")
    public Person getPerson() {
       return person;
    }
 
    public void setPerson(Person person) {
       this.person = person;
    }
 }








Hibernate-----一对一唯一外键关联映射(注解)

标签:唯一外键   hibernate   注解   一对一关联映射   

原文地址:http://blog.csdn.net/hekewangzi/article/details/41594473

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