Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Exception : detached entity passed to persist [duplicate]

Tags:

People also ask

How do you resolve a detached entity passed to persist?

The solution is simple, just use the CascadeType. MERGE instead of CascadeType. PERSIST or CascadeType. ALL .

What is the meaning of detached entity passed to persist?

Detached Entities A detached entity is a Java object that is no longer tracked by the persistence context. Entities can reach this state if we close or clear the session.

What is detached entity?

A detached entity is just an ordinary entity POJO whose identity value corresponds to a database row. The difference from a managed entity is that it's not tracked anymore by any persistence context. An entity can become detached when the Session used to load it was closed, or when we call Session.

How do you persist many to many relationships in JPA?

In JPA we use the @ManyToMany annotation to model many-to-many relationships. This type of relationship can be unidirectional or bidirectional: In a unidirectional relationship only one entity in the relationship points the other. In a bidirectional relationship both entities point to each other.


I'm trying to insert a person pojo to mysql db using Hibernate EntityManager persist method,

entityManagerTransactionService.getEntityManager().persist(TemplateObject);

and getting this exception,

 javax.persistence.PersistenceException:
 org.hibernate.PersistentObjectException: detached entity passed to
 persist: com.esupu.base.model.pojo.common.TitleType    at
 org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)....

and there is more..

 Caused by: org.hibernate.PersistentObjectException: detached entity
 passed to persist: com.esupu.base.model.pojo.common.TitleType  at
 org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
    at
 org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:843)

My Person.java class is,

@Entity
public class Person extends TemplateObject {

    @OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST,CascadeType.REFRESH })
        private TitleType titleType;
        private String middle;
        private String last;
        @OneToOne(cascade = { CascadeType.ALL })
        private Address address;
        private Date birthdate;
        private String email;
        private String telephone;
        private String fax;
        @OneToOne(cascade = { CascadeType.ALL })
        private Location location;
        @OneToOne(cascade = { CascadeType.ALL })
        private Country country;

and then the getter setter methods of the pojo ..

the error throwing TitleType class is a "public static final" title type definition provider,

@Entity
public class TitleType extends TemplateObject {

    /**
     * 
     */
    private static final long serialVersionUID = 3104954552111621034L;

    private TitleType(int id, String name) {
        setId(id);
        setName(name);
    }

    public static final TitleType MR = new TitleType(1, "Mr");
    public static final TitleType MISS = new TitleType(2, "Miss");
    public static final TitleType MRS = new TitleType(3, "Mrs");
    public static final TitleType MS = new TitleType(4, "Ms");
    public static final TitleType DOCTOR = new TitleType(5, "Doctor");
    public static final TitleType PROFESSOR = new TitleType(6, "Professor");
    public static final TitleType SIR = new TitleType(7, "Sir");
    public static final TitleType MADAM = new TitleType(8, "Madam");

}

I'm setting the title type class to the the person as,

person.setTitleType(TitleType.MR);

is it impossible to pass a public static final type defined class to Hibernate? or is there anything that I'm doing wrong?

Thanks in advance