繁体   English   中英

如何使用hibernate更新实体

[英]How to update entity using hibernate

我有实体用户:

@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

基础对象是:@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;
}

}

问题是,当我尝试UPDATE对象时,我收到错误:

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'

对于我使用的更新实体:

@Transactional
public void save(User value){
   em.merge(value);
}

我不知道为什么我得到这个错误,我怎么能更新我的对象? 谢谢您的帮助。

编辑:我更新:

User u = em.find(persistedType, id); //get user by ID
u.getSetOfRequesSet().add(requestSet); //add one otem to set
userDap.merge(u); //save

看起来您的更新会复制集合中的条目(em.merge)。 为防止重复,您应该明确合并set。 做如下。

  1. 将方法合并添加到用户:

     protected User merge(User other) { // Just assign all data members(2 examples here) setEmail(other.getEmail()); .... setSetOfRequesSet(other.getSetOfRequesSet()); ... } 
  2. 准备更新的对象实例:

     User updated = em.find(persistedType, id); //get user by ID updated.getSetOfRequesSet().add(requestSet); //add one item to set 
  3. 更新现有的db实体:

     User existing = em.find(persistedType, id); existing.merge(updated); 

更新后无需调用em.merge,因为保存将在事务结束时完成。

你可以在这里找到解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM