我对如何使用连接列在db中保存entiry感到困惑。我有@Entity吼叫
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
@Entity
@Table(name = "psc_users")
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 8885916014620036457L;
@Id
private static final String SEQUENCE_NAME = "psc_users_user_id_seq";
@Id
@GeneratedValue(generator = "UseExistingOrGenerateIdGenerator",
strategy = GenerationType.SEQUENCE)
@GenericGenerator(name = "UseExistingOrGenerateIdGenerator",
strategy = "com.psc.util.UseExistingOrGenerateIdGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = "sequence", value = SEQUENCE_NAME)
}
)
@Column(name = "USER_ID")
private Long userId;
@Column(name = "DEF", length = 30)
private String def;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DEL_DATE")
private Date delDate;
@Column(name = "DISPLAY_DEF", length = 60)
private String displayDef;
@Column(name = "EMAIL", length = 60)
private String email;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "NAVI_DATE")
private Date naviDate;
@Column(name = "NAVI_USER")
private String naviUser;
@Column(name = "PHONE", length = 30)
private String phone;
@Column(name = "PWD", length = 40)
private String pwd;
//bi-directional many-to-one association to Branch
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "BRNC_BRNC_ID", nullable = false)
private Branch pscBranch;
public Long getBrncBrncId() {
return brncBrncId;
}
public void setBrncBrncId(Long brncBrncId) {
this.brncBrncId = brncBrncId;
}
@Column(name = "BRNC_BRNC_ID", insertable = false, updatable = false)
private Long brncBrncId;
//bi-directional many-to-one association to User
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "CURATOR_USER_ID")
private User pscUser;
public Long getCuratorUserId() {
return curatorUserId;
}
public void setCuratorUserId(Long curatorUserId) {
this.curatorUserId = curatorUserId;
}
@Column(name = "CURATOR_USER_ID", insertable = false, updatable = false)
private Long curatorUserId;
public User() {
}
public Long getUserId() {
return this.userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getDef() {
return this.def;
}
public void setDef(String def) {
this.def = def;
}
public Date getDelDate() {
return this.delDate;
}
public void setDelDate(Date delDate) {
this.delDate = delDate;
}
public String getDisplayDef() {
return this.displayDef;
}
public void setDisplayDef(String displayDef) {
this.displayDef = displayDef;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getNaviDate() {
return this.naviDate;
}
public void setNaviDate(Date naviDate) {
this.naviDate = naviDate;
}
public String getNaviUser() {
return this.naviUser;
}
public void setNaviUser(String naviUser) {
this.naviUser = naviUser;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPwd() {
return this.pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Branch getPscBranch() {
return this.pscBranch;
}
public void setPscBranch(Branch pscBranch) {
this.pscBranch = pscBranch;
}
public User getPscUser() {
return this.pscUser;
}
public void setPscUser(User pscUser) {
this.pscUser = pscUser;
}
}
如果我保存没有字段pscUser的用户实例(此处为null)但是有正确值的有效CuratorUserId我最终会在db中具有空CuratorUserId的情况下结束。如果您查看代码,那么您将看到这些绑定字段。
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "CURATOR_USER_ID")
private User pscUser;
@Column(name = "CURATOR_USER_ID", insertable = false, updatable = false)
private Long curatorUserId;
保存用户的代码
repositoryUser.save(user);
我在调试器中看到了
这是我在保存用户后在数据库中看到的。
抱歉我的愚蠢问题,但我遇到了不同的行为,我的项目中的代码以另一种方式表现。我不想搜索实际的另一个用户(策展人)来保存我的用户,因为查询的开销
答案 0 :(得分:1)
curetorUserId字段上的@Column注释具有属性 insertable = false和updatable = false,表示在插入和更新期间忽略其值。
你可以将这些属性更改为true(但它可以在其他一些地方破坏你的应用程序)或者只使用EntityManager.getReference填充pscUser字段,它只是创建一个代理并且不会实际生成对数据库的查询
答案 1 :(得分:1)
您的映射应如下所示:
@XmlRootElement
@XmlAccessorType(value = XmlAccessType.FIELD)
@Entity
@Table(name = "psc_users")
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 8885916014620036457L;
@Id
private static final String SEQUENCE_NAME = "psc_users_user_id_seq";
@Id
@GeneratedValue(generator = "UseExistingOrGenerateIdGenerator",
strategy = GenerationType.SEQUENCE)
@GenericGenerator(name = "UseExistingOrGenerateIdGenerator",
strategy = "com.psc.util.UseExistingOrGenerateIdGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = "sequence", value = SEQUENCE_NAME)
}
)
@Column(name = "USER_ID")
private Long userId;
@Column(name = "DEF", length = 30)
private String def;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DEL_DATE")
private Date delDate;
@Column(name = "DISPLAY_DEF", length = 60)
private String displayDef;
@Column(name = "EMAIL", length = 60)
private String email;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "NAVI_DATE")
private Date naviDate;
@Column(name = "NAVI_USER")
private String naviUser;
@Column(name = "PHONE", length = 30)
private String phone;
@Column(name = "PWD", length = 40)
private String pwd;
//bi-directional many-to-one association to Branch
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "BRNC_BRNC_ID", nullable = false)
private Branch pscBranch;
//bi-directional many-to-one association to User
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "CURATOR_USER_ID")
private User pscUser;
public User() {
}
}
你需要从对象的角度思考。如果将pscUser引用设置为用户实例,则只能在数据库中设置FK。如果这是现有用户,则需要设置对现有持久实体的引用。
答案 2 :(得分:0)