title: detached entity passed to persist
body: <p>used: hibernate 3.6.10, maven 2, postgres 9.
  I have code that must work but it doesn't.
  before I used hibernate 3.6.2 and got really frustrated error:
  java.lang.ClassCastException: org.hibernate.action.DelayedPostInsertIdentifier cannot be cast to java.lang.Long</p>
  
  <p>But after updating to 3.6.10 error became more sensible:
  javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist</p>
  
  <p>Code is a standart domain model:</p>
  
  <p>Entity:</p>
  
  <pre><code>@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
  @Entity
  @Table(schema = "simulators", name = "mySimulator_card")
  public class MySimulatorCard {
  
      @Id
      @Column(name = "id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Column(name = "account_number", unique = true, nullable = false)
      private String accountNumber;
  </code></pre>
  
  <p>etc...</p>
  
  <p>DAO:</p>
  
  <pre><code>public abstract class AbstractDao&lt;E, PK extends Serializable&gt; implements Dao&lt;E, PK&gt; {
  
      private EntityManager entityManager;
  
      public EntityManager getEntityManager() {
          return entityManager;
      }
  
      @PersistenceContext(unitName = "MySimulator")
      public void setEntityManager(EntityManager entityManager) {
          this.entityManager = entityManager;
      }
  
      public abstract Class&lt;E&gt; getEntityClass();
  
      @Override
      public void persist(E e) {
          getEntityManager().persist(e);//&lt;-- some thing wroooong
      }
  </code></pre>
  
  <p>etc...</p>
  
  <p>And according table:</p>
  
  <pre><code>CREATE TABLE simulators.mySimulator_card
  (
    id bigserial NOT NULL,
    account_number character varying(255) NOT NULL,
  
  etc...
  
    CONSTRAINT mySimulator_card_pk PRIMARY KEY (id),
    CONSTRAINT mySimulator_card_account_fk FOREIGN KEY (account_id)
        REFERENCES simulators.mySimulator_account (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT mySimulator_card_currency_fk FOREIGN KEY (currency_id)
        REFERENCES simulators.mySimulator_currency ("name") MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT mySimulator_card_product_fk FOREIGN KEY (product_id)
        REFERENCES simulators.mySimulator_product (id) MATCH SIMPLE
        ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT mySimulator_account_account_number_uq UNIQUE (account_number),
    CONSTRAINT mySimulator_card_san_uq UNIQUE (san)
  )
  WITH (
    OIDS=FALSE
  );
  ALTER TABLE simulators.mySimulator_card OWNER TO functional;
  </code></pre>
  
  <p>I suppose something wrong with context in functional tests because production is work fine. Also when I tried to use this trick to get session from entityManager:</p>
  
  <pre><code>    EntityManager em = getEntityManager();
      HibernateEntityManager hem = em.unwrap(HibernateEntityManager.class);
      Session session = hem.getSession();
      session.persist(e);
  </code></pre>
  
  <p>I've got the error:</p>
  
  <p>java.lang.IllegalStateException: No transactional EntityManager available</p>
  
  <p>Now sure that it is a good idea to post all test config because there are a lot of workaround stuff.
  Any idea?</p>