Reputation:
I'm getting this error when I build my Application:
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: model.Student
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753) [hibernate-core-5.0.7.Final.jar:5.0.7.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146) [hibernate-entitymanager-5.0.7.Final.jar:5.0.7.Final]
... 190 more
I have to add that, when I use merge(std) instead of persist(std). Than i have no problem which is also weird.
Thats my Entity class with GeneratedValue --> GenerationType.AUTO):
@Entity
@NamedQueries({@NamedQuery(name = Student.QRY_GET_STUDENTS, query = "select s from Student s")})
public class Student {
public static final String QRY_GET_STUDENTS = "studentQuery";
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min = 3, max = 70)
private String name;
@Size(min = 7, max = 7, message = "Matrikelnummer muss 7 Ziffern haben!")
private String matriculationNumber;
@DecimalMax(value = "5.0", message = "Note muss zwischen 1.0 und 5.0 sein!")
private double note;
private String lecture;
}
Thats my persistence class:
@Stateless
@Transactional
public class StudentPersistence {
@PersistenceContext(unitName = "UniPortalDS")
private EntityManager em;
public List<Student> getStudent() {
return (List<Student>) em.createNamedQuery("studentQuery").getResultList();
}
public List<Student> saveAllStudentsPersistence(@Nonnull Student std) {
TypedQuery<Student> q = em.createNamedQuery(std.QRY_GET_STUDENTS, Student.class);
em.persist(std);
return q.getResultList();
}
}
Finally thats my service class:
@Stateless
public class StudentService {
@EJB
private StudentPersistence studentPersistence;
public List<Student> getStudent() {
return studentPersistence.getStudent();
}
//Jetzt -->
public void saveStudentsNew(@Nonnull Student std) {
studentPersistence.saveAllStudentsPersistence(std);
}
}
Upvotes: 0
Views: 10102
Reputation: 22514
In JPA, "non-persistent" and "detached" are not the same thing:
non-persistent: these entities do not currently exist in the DB. You can take a non-persistent entity, invoke persist()
on it, and that should make it persistent (that is, store it in the DB).
detached: these entities do exist in the DB, but are not currently being managed be the EntityManager
. It makes no sense to persist()
them because although they are currently detached they are already persistent, and I think this is the error that Hibernate is communicating to you. However, you can merge()
them, that is, synchronize them with the persistence context and turning them into entities managed by the EntityManager
so that changes are sent to the DB (1).
(1) Actually, merge()
does not transition the entity from detached to managed; what it does is take some entity in whatever state it is, and return a corresponding managed instance. The original instance remains in whatever state it was.
Upvotes: 1