java 如何在 Hibernate 中设置只读列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14626341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set read-only columns in Hibernate?
提问by cloudy_weather
I don't know how to force read-only columns in Hibernate.
我不知道如何在 Hibernate 中强制只读列。
I would like to set idgroup as read only column. Even if I set insertable=false
and updatable=false
, in the hibernate SQL I can read:
我想将 idgroup 设置为只读列。即使我设置了insertable=false
and updatable=false
,在休眠 SQL 中我也可以阅读:
Hibernate: insert into groups (description, name, account_idaccount, idgroup) values (?, ?, ?, ?)
but I would like to obtain:
但我想获得:
insert into groups (description, name, account_idaccount) values (?, ?, ?)
Here are my classes:
这是我的课程:
@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;
}
..
}
I would like to have a read only column for idgroup because I can exploit the id auto-generation of the DBMS, I do not want to use the key auto-generation in Hibernate because it is not cluster-safe.
我想为 idgroup 设置一个只读列,因为我可以利用 DBMS 的 id 自动生成,我不想在 Hibernate 中使用密钥自动生成,因为它不是集群安全的。
采纳答案by cloudy_weather
The solution is using wrapper classes, such as Integer
, not primitivetypes like int
.
When Hibernate will get a null pointer it will delegate the generation of the id to the DBMS.
解决方案是使用包装类,例如Integer
,而不是像int
. 当 Hibernate 得到一个空指针时,它将把 id 的生成委托给 DBMS。
回答by Bob Flannigon
I think you can mark an @Column
annotation as updatable=false
我认为您可以将@Column
注释标记为updatable=false
回答by J. Jerez
Easy: @Column(insertable = false, updatable = false)
简单:@Column(可插入 = 假,可更新 = 假)
回答by jDub9
I was able to achieve this by using 2 DAO objects. I know it may not be preferred, but I didn't see another way around it. Setting the field to insertable=false
and updatable=false
do not remove the columns from the insert SQL so Postgres will not invoke the default
action to populate those column values.
我能够通过使用 2 个 DAO 对象来实现这一点。我知道它可能不是首选,但我没有看到其他方法。将字段设置为insertable=false
并且updatable=false
不从插入 SQL 中删除列,因此 Postgres 不会调用default
操作来填充这些列值。
// GroupWrite
@Entity
@Table(name = "group")
public class GroupWrite {
@Id
@Column(name = "account")
private Account account;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
/* Getters and Setters */
}
--
——
// GroupRead
@Entity
@Table(name = "group")
public class GroupRead {
@EmbeddedId
@Column(name = "id")
private GroupId id;
@Column(name = "account")
private Account account;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
/* Getters and Setters */
}
Then you need to use the GroupRead
when using select
queries and GroupWrite
when doing insert
or update
queries.
然后你需要使用GroupRead
when using select
queries 和GroupWrite
when doinsert
或update
查询。
If you want to use attributes from both in a common way you can implement an interface that describes the overlapping fields.
如果您想以通用方式使用两者的属性,您可以实现一个描述重叠字段的接口。
public interface Group {
Account getAccount();
void setAccount(Account account);
}
public class GroupWrite implements Group {...}
public class GroupRead implements Group {...}
Hope this helps.
希望这可以帮助。
回答by Rakesh
@ReadOnlyProperty annotation will help you in this case.
在这种情况下,@ReadOnlyProperty 注释将为您提供帮助。