直接上代码,一目了然, 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;
}
}