hibernate_one2one共享主键并延迟加载(注解形式)

直接上代码,一目了然, User UserDetail


import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
* User entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "user")
public class User implements java.io.Serializable {

private Integer uid;
private String username;
private String password;
private String realname;
private UserDetail userDetail;

public User() {
userDetail = new UserDetail();
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "uid", unique = true, nullable = false)
public Integer getUid() {
return this.uid;
}

public void setUid(Integer uid) {
this.uid = uid;
}

@Column(name = "username", length = 30)
public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

@Column(name = "password", length = 30)
public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

@Column(name = "realname", length = 10)
public String getRealname() {
return this.realname;
}

public void setRealname(String realname) {
this.realname = realname;
}

//========================= 注意这里 ====================================
@OneToOne(fetch = FetchType.LAZY, optional = false)
@PrimaryKeyJoinColumn
public UserDetail getUserDetail() {
return this.userDetail;
}

public void setUserDetail(UserDetail userDetail) {
this.userDetail = userDetail;
}
//=======================================================================

}




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.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.GenericGenerator;

/**
* UserDetail entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "user_detail")
public class UserDetail implements java.io.Serializable {

private Integer udid;
private User user;
private String description;

public UserDetail() {
}

@Id
@GeneratedValue(generator = "pkGenerator")
@GenericGenerator(
name = "pkGenerator",
strategy = "foreign",
parameters = @Parameter(name = "property", value = "user")
)
@Column(name="udid")
public Integer getUdid() {
return this.udid;
}

public void setUdid(Integer udid) {
this.udid = udid;
}

//========================= 注意这里 ====================================
@OneToOne(mappedBy="userDetail", fetch=FetchType.LAZY, optional=false)
public User getUser() {
return this.user;
}

public void setUser(User user) {
this.user = user;
}
//=======================================================================

@Column(name = "description", length = 400)
public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值