title: Why hibernate runs delete and insert commands for embaddable objects
body: <p>I am trying to create a simple example to understand how collection of basic and embaddable types works in Hibernate.</p>
  
  <p>I have created a <code>User</code> entity with a set of <code>nickNames</code> and also a set of <code>addresses</code>. Here are my Java classes:</p>
  
  <p><code>User.java</code></p>
  
  <pre><code>@Entity
  @Table(name = "TB_User")
  public class User {
      @Id
      @GeneratedValue
      private int id;
      private String name;
      @ElementCollection
      @CollectionTable(name = "Nicknames", joinColumns = @JoinColumn(name = "user_id"))
      @Column(name = "nickname")
      private Set&lt;String&gt; nickNames = new HashSet&lt;String&gt;();
      @ElementCollection
      @CollectionTable(name = "Addresses", joinColumns = @JoinColumn(name = "user_id"))
      @AttributeOverrides({ @AttributeOverride(name = "street1", column = @Column(name = "fld_street")) })
      public Set&lt;Address&gt; addresses = new HashSet&lt;Address&gt;();
  
      public User() {
      }
  
      public User(String name, Address... addresses) {
          this.name = name;
          this.addresses.addAll(Arrays.asList(addresses));
      }
  
      public void addNickName(String... nickNames) {
          this.nickNames.addAll(Arrays.asList(nickNames));
      }
  // Setters &amp; Getters
  }
  </code></pre>
  
  <p><code>Address.java</code></p>
  
  <pre><code>@Embeddable
  public class Address {
      private String street1;
      public Address() {}
      public Address(String street1) {
          this.street1 = street1;
      }
      @Override
      public String toString() {
          return street1;
      }
  // Setters &amp; Getters
  }
  </code></pre>
  
  <p>Now I have created a simple program to create users in database and then show the list of users. Here is my code:</p>
  
  <pre><code>private static void saveUsers() {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.getTransaction().begin();
          User user = new User("User", new Address("abc"),
                  new Address("xyz"));
          user1.addNickName("alpha", "beta");
          session.save(user);
          session.getTransaction().commit();
      }
  
  private static void showUsers() {
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.getTransaction().begin();
      List&lt;User&gt; users = session.createQuery("from User").list();   
      for (User user : users) {
          System.out.println(user.getName() + " -- &gt; " + user.getNickNames()
                  + " --&gt; " + user.getAddresses());
      }    
      session.getTransaction().commit();
  }
  </code></pre>
  
  <p>When I run this program, I observed that hibernate issues below set of commands:</p>
  
  <pre><code>Hibernate: select user0_.id as id1_2_, user0_.name as name2_2_ from TB_User user0_
  Hibernate: select nicknames0_.user_id as user_id1_2_0_, nicknames0_.nickname as nickname2_1_0_ from Nicknames nicknames0_ where nicknames0_.user_id=?
  Hibernate: select addresses0_.user_id as user_id1_2_0_, addresses0_.fld_street as fld_street2_0_0_ from Addresses addresses0_ where addresses0_.user_id=?
  User -- &gt; [alpha, beta] --&gt; [xyz, abc]
  
  Hibernate: delete from Addresses where user_id=?
  Hibernate: insert into Addresses (user_id, fld_street) values (?, ?)
  Hibernate: delete from Addresses where user_id=?
  Hibernate: insert into Addresses (user_id, fld_street) values (?, ?)
  </code></pre>
  
  <p>If I try to get the list of addresses using <code>user.getAddresses()</code> with in <code>session</code>, then Hibernate deletes &amp; re-inserts records in <code>Addresses</code> table.</p>
  
  <p>Why hibernate tries to delete and re-create records in <code>Addresses</code> table, as this causes performance issue. Also why it is not applicable to basic types like <code>nickNames</code> in my example and not running update commands for the property <code>nickNames</code>?</p>