Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

detached entity passed to persist

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

But after updating to 3.6.10 error became more sensible: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist

Code is a standart domain model:

Entity:

@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;

etc...

DAO:

public abstract class AbstractDao<E, PK extends Serializable> implements Dao<E, PK> {

    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    @PersistenceContext(unitName = "MySimulator")
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public abstract Class<E> getEntityClass();

    @Override
    public void persist(E e) {
        getEntityManager().persist(e);//<-- some thing wroooong
    }

etc...

And according table:

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;

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:

    EntityManager em = getEntityManager();
    HibernateEntityManager hem = em.unwrap(HibernateEntityManager.class);
    Session session = hem.getSession();
    session.persist(e);

I've got the error:

java.lang.IllegalStateException: No transactional EntityManager available

Now sure that it is a good idea to post all test config because there are a lot of workaround stuff. Any idea?

like image 785
Dmitrii Borovoi Avatar asked Dec 28 '22 02:12

Dmitrii Borovoi


1 Answers

This error is raised among others when you try to persist a value in a column that is autogenerated.

Check if you're passing a value to persist for the column id, which you've marked as:

@GeneratedValue(strategy = GenerationType.IDENTITY)

Regards.

like image 175
periket2000 Avatar answered Dec 29 '22 16:12

periket2000