title: How to avoid creating a new row if similar row exists?
body: <p>I need to configure hibernate to avoid creating duplicate rows, (although the row exists it creates a new one, and since only one filed is set it set all the rest to NULL)</p>
  
  <p>Lets say I have a row as following </p>
  
  <pre><code>id des    index age
  1  MyName 2     23
  </code></pre>
  
  <p>Although I just set MyName as des and it already exists in the Name table hibernate 
  create a new row as following </p>
  
  <pre><code>id des    index age
  1  MyName 2     23
  2  MyName Null  Null     &lt;&lt; new row with null values will be created 
                              rather than updating the previous one
  </code></pre>
  
  <p>When I want to sa
   .
  So I have added the following annotation to my class but it crossed the Entity, and dynamicUpdate.</p>
  
  <pre><code>@org.hibernate.annotations.Entity(
    dynamicUpdate = true
  )
  </code></pre>
  
  <p>I used <code>@DynamicUpdate</code> as well, although hibernate accept it but still I have the same problem.</p>
  
  <p>Is there any other method to do it ? Version of my hibernate is as following </p>
  
  <pre><code>&lt;dependency&gt;
        &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
        &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
        &lt;version&gt;4.2.1.Final&lt;/version&gt;
        &lt;type&gt;jar&lt;/type&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>*<strong><em>Based on Ray's comment blew, By assigning a value to Id of child class it works properly but how about if I do not have the ID? do I have to do a select to find the id first ? Is there any method to force hibernate to do it automatically r based on values of child class rahter than doing a separate select to find the id? *</em></strong></p>
  
  <p><strong>User.Java</strong></p>
  
  <pre><code>....
  import org.hibernate.annotations.DynamicUpdate;
  
  @Entity
  @Table(name = "user") 
  @DynamicUpdate
  public class User implements Serializable {
  
    private int ID;
    private Name name;
    private String type;
  
    public User() {
    }
  
    @Id
    @GeneratedValue
    @Column(name = "id")
    public int getID() {
       return ID;
    }
  
    public void setID(int ID) {
       this.ID = ID;
    }
  
    @ManyToOne(cascade = CascadeType.ALL)
    public Name getName() {
       return name;
    }
  
    public void setName(Name name) {
      this.name = name;
    }
  
    .....
  </code></pre>
  
  <p><strong>Name.Java</strong></p>
  
  <pre><code>@Entity()
  @Table(name = "Name")
  public class Name implements Serializable {
  
  private int id;
  private String des;
  private String index;
  private String age;
  
  public Name() {
  }
  
  @Id
  @GeneratedValue
  @Column(name="id", unique= true, nullable = false)
  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  .....
  </code></pre>
  
  <p><strong>Model.java</strong></p>
  
  <pre><code>public void addMyUsers(){
     Name name = new Name();
     name.setDes("MyName");
     While( ..... )
     {
         User user = new User();
         user.setName(name);
         user.setType(X);
         addUser(user);
     }
  }
  
  public void addUser(User user) {
          session = Util.getSession();
  
          session.beginTransaction();
  
  
          session.merge(user); 
          //session.saveOrUpdate(user);
  
          session.getTransaction().commit();
  
          session.close();
  }
  </code></pre>