java 如何使用休眠更新实体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38930631/
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 update entity using hibernate
提问by
I have entity user:
我有实体用户:
@Entity
@Table(name = "entity_user")
@AttributeOverride(name = "id", column = @Column(name = "user_id"))
public class User extends BaseObject implements Serializable {
/**
* Login, unique
*/
private String email;
private String username;
/**
* Secret for signing-in
*/
private String password;
/**
* Type of user
*/
private UserType userType;
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinTable(name = "view_user_to_requestSet", joinColumns = {
@JoinColumn(name = "user_id")}, inverseJoinColumns = {
@JoinColumn(name = "requestSet_id")})
private Set<RequestSet> setOfRequesSet = new HashSet<>();
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinTable(name = "view_user_to_requestSetCreator", joinColumns = {
@JoinColumn(name = "user_id")}, inverseJoinColumns = {
@JoinColumn(name = "requestSet_id")})
private Set<RequestSet> setOfRequesSetCreator = new HashSet<>();
@OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinTable(name = "view_user_to_userTask", joinColumns = {
@JoinColumn(name = "user_id")}, inverseJoinColumns = {
@JoinColumn(name = "userTask_id")})
private Set<UserTask> setOfUserTask = new HashSet<>();
public User() {
}
.
.
..... GET AND SET
Base object is: @MappedSuperclass public class BaseObject {
基础对象是:@MappedSuperclass public class BaseObject {
@Id
@GeneratedValue
@Column(name = "entity_id")
private Long id;
/**
*
* @return true if the entity hasn't been persisted yet
*/
@Transient
public boolean isNew() {
return id == null;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
}
The proble is, when I try to UPDATE object, I am getting errors:
问题是,当我尝试更新对象时,出现错误:
Exception in thread "Thread-15" org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [UK_5fa729oayn43ynq4v2y4d9qcn]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '5' for key 'UK_5fa729oayn43ynq4v2y4d9qcn'
For update entity I am using:
对于我正在使用的更新实体:
@Transactional
public void save(User value){
em.merge(value);
}
I am dont know why I am getting this error, how can I update my object? Thank you for your help.
我不知道为什么会出现此错误,如何更新我的对象?谢谢您的帮助。
EDIT:I update:
编辑:我更新:
User u = em.find(persistedType, id); //get user by ID
u.getSetOfRequesSet().add(requestSet); //add one otem to set
userDap.merge(u); //save
采纳答案by asch
It looks like your update duplicates entries in the set (em.merge). To prevent the duplication you should merge set explicitly. Do as following.
看起来您的更新重复了集合中的条目 (em.merge)。为了防止重复,您应该明确合并集。请按以下步骤操作。
Add method merge to User:
protected User merge(User other) { // Just assign all data members(2 examples here) setEmail(other.getEmail()); .... setSetOfRequesSet(other.getSetOfRequesSet()); ... }
Prepare an updated object instance:
User updated = em.find(persistedType, id); //get user by ID updated.getSetOfRequesSet().add(requestSet); //add one item to set
Update the existing db entity:
User existing = em.find(persistedType, id); existing.merge(updated);
向用户添加方法合并:
protected User merge(User other) { // Just assign all data members(2 examples here) setEmail(other.getEmail()); .... setSetOfRequesSet(other.getSetOfRequesSet()); ... }
准备一个更新的对象实例:
User updated = em.find(persistedType, id); //get user by ID updated.getSetOfRequesSet().add(requestSet); //add one item to set
更新现有的数据库实体:
User existing = em.find(persistedType, id); existing.merge(updated);
Not necessary to cal to em.merge after the update, since save will be just done at the end of a transaction.
没有必要在更新后调用 em.merge,因为保存将在事务结束时完成。
You can find the explanation here.
您可以在此处找到说明。