Skip to content
Advertisement

Detached entity passed to persist java spring

So i have a big error when trying to do an update method in my spring application ,the problem is regarding to the Caracter class as thats the one the seems to generate it. When i set my stuff in main and try to update an Anime object in the database the Caracter class gives me the errors presented below

the code

JavaScript
JavaScript
JavaScript
JavaScript

and the error

JavaScript

Advertisement

Answer

I think that the issue here is, that you are replacing the characters collection with a new collection and the collection has different object than the persistence context, although it uses the same primary key. Since you are cascading changes, Hibernate tries to flush the changes and realizes that an entity for that primary key is already associated with the persistence context and fails to flush with the exception you are seeing. If you want to just apply the entity to the database, try to use entityManager.merge without loading the entity first. Alternatively, you could also try to apply the collections like this:

JavaScript
Advertisement