title: How to update entity using hibernate
body: <p>I have entity user:</p>
  
  <pre><code>@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&lt;RequestSet&gt; setOfRequesSet = new HashSet&lt;&gt;();
  
      @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&lt;RequestSet&gt; setOfRequesSetCreator = new HashSet&lt;&gt;();
  
      @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&lt;UserTask&gt; setOfUserTask = new HashSet&lt;&gt;();
  
      public User() {
      }
  
      .
      .
  ..... GET AND SET
  </code></pre>
  
  <p>Base object is:
  @MappedSuperclass
  public class BaseObject {</p>
  
  <pre><code>@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;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The proble is, when I try to UPDATE object, I am getting errors:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>For update entity I am using:</p>
  
  <pre><code>@Transactional
  public void save(User value){
     em.merge(value);
  }
  </code></pre>
  
  <p>I am dont know why I am getting this error, how can I update my object? Thank you for your help.</p>
  
  <p><strong>EDIT:</strong>
  I update:</p>
  
  <pre><code>User u = em.find(persistedType, id); //get user by ID
  u.getSetOfRequesSet().add(requestSet); //add one otem to set
  userDap.merge(u); //save
  </code></pre>