我不知道如何在hibernate中强制只读列。
我想将idgroup设置为只读列。即使我设置了insertable=false
和updatable=false
,在hibernate 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中使用密钥自动生成,因为它不是集群安全的。
最佳答案
我认为您可以将@Column
注释标记为updatable=false