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

hibernate one2one

时间:2017-10-09 14:48:46      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:turn   net   getname   one   关系   ``   而不是   public   _id   

 

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

@Entity
public class Husband {

@Id
@GeneratedValue
private int id;
private String name;

 

@OneToOne     // (1   一对一关系    @OneToOne(mappedBy="husband")//在对应的 wife表中husband_id  对应当前主键id)        

                         //(2  mappedBy = "husband"-------对应的是  Wife类里面的 成员变量Husband husband--变量名  而不是getter方法名)

                         //(3 Wife 类 里面有对应的 Husband 成员变量 并却是 @OneToOne 关系)

@JoinColumn(name="wife_id")        //joinColumn 在当前表中 添加字段 (wife_id) 对应的外键id 为 wife里的主键id     

                                                         
private Wife wife;

public int getId() {
return id;
}

public String getName() {
return name;
}

public Wife getWife() {
return wife;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}

}

 

 

``````````````````````````````````````````````````````

 

package com.bjsxt.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;

@Entity
public class Wife {
@Id
@GeneratedValue
private int id;
private String name;
@OneToOne
private Husband husband;

public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

 

hibernate one2one

标签:turn   net   getname   one   关系   ``   而不是   public   _id   

原文地址:http://www.cnblogs.com/lize1215/p/7640998.html

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