我不知道如何在 Hibernate 中强制只读列。
我想将 idgroup 设置为只读列。即使我设置insertable=false
and updatable=false
,在休眠 SQL 中我也可以阅读:
Hibernate: insert into groups (description, name, account_idaccount, idgroup) values (?, ?, ?, ?)
但我想获得:
insert into groups (description, name, account_idaccount) values (?, ?, ?)
这是我的课程:
@Entity
@Table(name = "groups")
public class Group implements java.io.Serializable {
private static final long serialVersionUID = -2948610975819234753L;
private GroupId id;
private Account account;
private String name;
private String description;
@EmbeddedId
@AttributeOverrides({@AttributeOverride(name = "idgroup", column = @Column(name = "idgroup", insertable = false)),
@AttributeOverride(name = "accountIdaccount", column = @Column(name = "account_idaccount", nullable = false))})
public GroupId getId() {
return id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "account_idaccount", nullable = false, insertable = false, updatable = false)
public Account getAccount() {
return account;
}
@Column(name = "description", length = 512)
public String getDescription() {
return description;
}
@Column(name = "name", nullable = false, length = 128)
public String getName() {
return name;
}
..
}
@Embeddable
public class GroupId implements java.io.Serializable {
private int idgroup;
private int accountIdaccount;
@Column(name = "idgroup", insertable= false, updatable= false)
public int getIdgroup() {
return this.idgroup;
}
@Column(name = "account_idaccount", nullable = false)
public int getAccountIdaccount() {
return this.accountIdaccount;
}
..
}
我想为 idgroup 设置一个只读列,因为我可以利用 DBMS 的 id 自动生成,我不想在 Hibernate 中使用密钥自动生成,因为它不是集群安全的。