Persistentobjectexception: Detached Entity Passed to Persist Thrown by JPA and Hibernate

org.hibernate.PersistentObjectException: detached entity passed to persist

You didn't provide many relevant details so I will guess that you called getInvoice and then you used result object to set some values and call save with assumption that your object changes will be saved.

However, persist operation is intended for brand new transient objects and it fails if id is already assigned. In your case you probably want to call saveOrUpdate instead of persist.

You can find some discussion and references here "detached entity passed to persist error" with JPA/EJB code

PersistentObjectException: detached entity passed to persist thrown by JPA and Hibernate

This is a typical bidirectional consistency problem. It is well discussed in this link as well as this link.

As per the articles in the previous 2 links you need to fix your setters in both sides of the bidirectional relationship. An example setter for the One side is in this link.

An example setter for the Many side is in this link.

After you correct your setters you want to declare the Entity access type to be "Property". Best practice to declare "Property" access type is to move ALL the annotations from the member properties to the corresponding getters. A big word of caution is not to mix "Field" and "Property" access types within the entity class otherwise the behavior is undefined by the JSR-317 specifications.

org.hibernate.PersistentObjectException: detached entity passed to persist error that I can't work my head around

The problem is, that you're trying to persist an entity with id being set and Hibernate thinks it's an existing entity, but can't find it in it's Persistence Context. Try to persist the entity without the ID, it should be generated automatically.

Detached entity passed to persist when save the child data

teamService.save(team);

Save method accepts only transient objects. What is the transient object you can find here

Transient - an object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore. Use the Hibernate Session to make an object persistent (and let Hibernate take care of the SQL statements that need to be executed for this transition).

You are getting the Team object and you are trying to persist it to the DB but that object has Account object in it and that Account object is detached (means that instance of that object has saved into the DB but that object is not in the session). Hibernate is trying to save it because of you have specified:

@OneToMany(cascade = CascadeType.ALL, ....

So, there are few ways how you can fix it:

1) do not use CascadeType.ALL configuration. Account object can be used for number of Teams (at least domain structure allows it) and update operation might update Account for ALL Teams -- it means that this operation should not be initiated with Team update.
I would remove cascade parameter from there (default value is no cascade operations), of if you really need use MERGE/DELETE configuration. But if you really need to persist it then see option #2

2) use 'saveOrUpdate()' method instead of 'save()'. 'saveOrUpdate()' method accepts transient and detached objects.
But the problem with this approach is in design: do you really need to insert/update account when you are saving Team object? I would split it in two operations and prevent updating Account from the Team.

Hope this helps.

org.hibernate.PersistentObjectException: detached entity passed to persist exception

Try with the below code then, it will allow you to set the ID manually.

Just use the @Id annotation which lets you define which property is the identifier of your entity. You don't need to use the @GeneratedValue annotation if you do not want hibernate to generate this property for you.

assigned - lets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.

package view;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "People")
public class Person {
@Id
//@GeneratedValue(strategy = GenerationType.AUTO) // commented for manually set the id
private int id;

private String name;
private String lastName;

public Person(int id, String name, String lastName) {
this.id = id;
this.name = name;
this.lastName = lastName;
}

public Person() {
}
}

JPA/Hibernate detached entity passed to persist

Looking at your Employee entity, you have configured Department to Cascade ALl
which means whenever Employee entity is PERSIST, MERGE, REMOVE, REFRESH, DETACH will do the same for Department.

Now coming back to your problem. In your populateTable function
you have created Department and Employee entity
you need to link Department and Employee like this

e1.setDepartment(d1);

and finally persist employee which will persist both Employee and Department for you

Spring data/Hibernate: detached entity passed to persist

Since you have @Transactional on private method and also one called from the same class, there is no transaction and therefore detached state for save (see this).
Not sure if you can apply @Transactional on run() or this class.
Anyway, maybe you should create new "Service" class with public @Transactional method, and than call this service from your class.



Related Topics



Leave a reply



Submit