hibernate 的OneToOne注解是把两张表关联起来
下面是案例:
把User(用户表)表和Customer(客户表)关联起来
User表对应的实体类如下:
package com.pms.login.pojo;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
@Entity
@Table(name="MAPS_MST_STAFF")
@SequenceGenerator (name = "SEQ", sequenceName = "MST_STAFF_SEQ", allocationSize = 1)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ")
@Column(name = "id")
private Integer id;
//员工编号
@Column(name="STAFF_ID")
private String staffId;
//员工姓名
@Column(name="STAFF_NAME")
private String staffName;
//登录名
@Column(name="STAFF_LOGIN_NAME")
private String staffLoginName;
//员工角色
@Column(name="STAFF_CHARACTOR")
private String staffCharactor;
//开始时间
@Column(name="START_DT")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date startDate;
//结束时间
@Column(name="END_DT")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date endDate;
//联系电话
@Column(name="STAFF_TEL")
private String staffTel;
//员工性别
@Column(name="STAFF_SEX")
private String staffSex;
//密码
@Column(name="STAFF_PASS")
private String staffPass;
//密码有效期
@Column(name="PASS_END_DT")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date passEndDate;
//是否能登陆
@Column(name="LOGIN_FLG")
private String loginFlg;
public User(){
}
public User(Integer id, String staffId, String staffName, String staffLoginName, String staffCharactor,
Date startDate, Date endDate, String staffTel, String staffSex, String staffPass, Date passEndDate,
String loginFlg) {
super();
this.id = id;
this.staffId = staffId;
this.staffName = staffName;
this.staffLoginName = staffLoginName;
this.staffCharactor = staffCharactor;
this.startDate = startDate;
this.endDate = endDate;
this.staffTel = staffTel;
this.staffSex = staffSex;
this.staffPass = staffPass;
this.passEndDate = passEndDate;
this.loginFlg = loginFlg;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStaffId() {
return staffId;
}
public void setStaffId(String staffId) {
this.staffId = staffId;
}
public String getStaffName() {
return staffName;
}
public void setStaffName(String staffName) {
this.staffName = staffName;
}
public String getStaffLoginName() {
return staffLoginName;
}
public void setStaffLoginName(String staffLoginName) {
this.staffLoginName = staffLoginName;
}
public String getStaffCharactor() {
return staffCharactor;
}
public void setStaffCharactor(String staffCharactor) {
this.staffCharactor = staffCharactor;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public String getStaffTel() {
return staffTel;
}
public void setStaffTel(String staffTel) {
this.staffTel = staffTel;
}
public String getStaffSex() {
return staffSex;
}
public void setStaffSex(String staffSex) {
this.staffSex = staffSex;
}
public String getStaffPass() {
return staffPass;
}
public void setStaffPass(String staffPass) {
this.staffPass = staffPass;
}
public Date getPassEndDate() {
return passEndDate;
}
public void setPassEndDate(Date passEndDate) {
this.passEndDate = passEndDate;
}
public String getLoginFlg() {
return loginFlg;
}
public void setLoginFlg(String loginFlg) {
this.loginFlg = loginFlg;
}
@Override
public String toString() {
return "User [id=" + id + ", staffId=" + staffId + ", staffName=" + staffName + ", staffLoginName="
+ staffLoginName + ", staffCharactor=" + staffCharactor + ", startDate=" + startDate + ", endDate="
+ endDate + ", staffTel=" + staffTel + ", staffSex=" + staffSex + ", staffPass=" + staffPass
+ ", passEndDate=" + passEndDate + ", loginFlg=" + loginFlg + "]";
}
}
这表没什么特别的,关键是customer表实体类:
package com.pms.projectsys.pojo;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.pms.login.pojo.User;
@Entity
@Table(name="MAPS_MST_PJ_CUSTOMER")
@SequenceGenerator (name = "CUSTOMER_SEQ", sequenceName = "MAPS_MST_PJ_CUSTOMER_SEQ", allocationSize = 1)
public class Customer {
//客户ID
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CUSTOMER_SEQ")
@Column(name = "CUSTOMER_ID")
private Integer customerId;
//客户名称
@Column(name="CUSTOMER_NAME")
private String customerName;
//创建时间
@Column(name="CREATION_TIME")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date creationTime;
//创建人ID
@Column(name="CREATION_USER_ID",updatable=false)
private Integer creationUserId;
//修改时间
@Column(name="MODIFIED_TIME")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date modifiedTime;
//修改人ID
@Column(name="MODIFIED_USER_ID")
private Integer modifiedUserId;
@OneToOne(optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "creation_user_id",referencedColumnName="id",updatable=false,insertable=false)
private User creationUser;
public User getCreationUser() {
return creationUser;
}
public void setCreationUser(User creationUser) {
this.creationUser = creationUser;
}
@OneToOne(optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "modified_user_id",referencedColumnName="id",updatable=false,insertable=false)
private User modifiedUser;
@javax.persistence.Transient
private String creationUserName;
public String getCreationUserName() {
return creationUserName;
}
public void setCreationUserName(String creationUserName) {
this.creationUserName = creationUserName;
}
@javax.persistence.Transient
private String modifiedUserName;
public String getModifiedUserName() {
return modifiedUserName;
}
public void setModifiedUserName(String modifiedUserName) {
this.modifiedUserName = modifiedUserName;
}
@Override
public String toString() {
return "Customer [customerId=" + customerId + ", customerName=" + customerName + ", creationTime="
+ creationTime + ", creationUserId=" + creationUserId + ", modifiedTime=" + modifiedTime
+ ", modifiedUserId=" + modifiedUserId + ", creationUser=" + creationUser + ", modifiedUser="
+ modifiedUser + ", creationUserName=" + creationUserName + ", modifiedUserName=" + modifiedUserName
+ "]";
}
public Customer(){
}
public Customer(Integer customerId, String customerName, User creationUser, Date creationTime,
User modifiedUser,Date modifiedTime) {
super();
this.customerId = customerId;
this.customerName = customerName;
this.creationTime = creationTime;
this.modifiedTime = modifiedTime;
this.creationUserId=creationUser.getId();
this.modifiedUserId=modifiedUser.getId();
this.creationUserName=creationUser.getStaffName();
this.modifiedUserName=modifiedUser.getStaffName();
}
public User getModifiedUser() {
return modifiedUser;
}
public void setModifiedUser(User modifiedUser) {
this.modifiedUser = modifiedUser;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public Date getCreationTime() {
return creationTime;
}
public void setCreationTime(Date creationTime) {
this.creationTime = creationTime;
}
public Integer getCreationUserId() {
return creationUserId;
}
public void setCreationUserId(Integer creationUserId) {
this.creationUserId = creationUserId;
}
public Date getModifiedTime() {
return modifiedTime;
}
public void setModifiedTime(Date modifiedTime) {
this.modifiedTime = modifiedTime;
}
public Integer getModifiedUserId() {
return modifiedUserId;
}
public void setModifiedUserId(Integer modifiedUserId) {
this.modifiedUserId = modifiedUserId;
}
}
在Customer实体类里面
增加一个Use类型的属性,
@OneToOne(optional = true, cascade = CascadeType.ALL)
@JoinColumn(name = "creation_user_id",referencedColumnName="id",updatable=false,insertable=false)
private User creationUser;
在属性上加上OneToOne注解,并且指明两个实体类表以哪个字段关联起来的,
@JoinColumn(name = "creation_user_id",referencedColumnName="id",updatable=false,insertable=false)
@JoinColumn这个注解属性里面:
name指向Customer对应的表里面所要关联的字段
referencedColumnName指向User对应的表,里面所要关联的字段
这样就把两张表通过字段关联起来了