title: StaleObjectStateException is encountered when an id is not set
body: <p>I have a DAO class with the following implementation:</p>
  <pre><code>@Override
      public void save(Employee emp) {        
          Session curSession = entityManager.unwrap(Session.class);       
          curSession.saveOrUpdate(emp);       
      }
  </code></pre>
  <p>and a service class with the below implementation:</p>
  <pre><code>@Override
      @Transactional
      public void save(Employee emp) {        
          employeeDAO.save(emp);
      }
  </code></pre>
  <p>When I execute this code it throws me a StaleObjectStateException - org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)</p>
  <p>But if I set the id before calling the DAO class, it works fine -</p>
  <pre><code>@Override
      @Transactional
      public void save(Employee emp) {
          emp.setId(0);
          employeeDAO.save(emp);
      }
  </code></pre>
  <p>Entity Class -</p>
  <pre><code>@Entity
  @Table(name=&quot;employee&quot;)
  public class Employee {
      
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      @Column(name=&quot;id&quot;)
      private int id;
      
      @Column(name=&quot;first_name&quot;)
      private String firstName;
      
      @Column(name=&quot;last_name&quot;)
      private String lastName;
      
      @Column(name=&quot;email&quot;)
      private String email;
      
      public Employee() {     
      }
  
      public Employee(int id, String firstName, String lastName, String email) {
          super();
          this.id = id;
          this.firstName = firstName;
          this.lastName = lastName;
          this.email = email;
      }
  //getters and setters for the fields are present
  </code></pre>
  <p>Can anyone help me understand what exactly is happening here?</p>