Spring data JPA and hibernate detached entity passed to persist on ManyToMany relationship

I had the same problem and solved it by removing the cascade = CascadeType.PERSIST.

In your case you use CascadeType.ALL, which is equivalent to also using the PERSIST, according to the documentation:

Defines the set of cascadable operations that are propagated to the associated entity. The value cascade=ALL is equivalent to cascade={PERSIST, MERGE, REMOVE, REFRESH, DETACH}.

It means when you try to save the reservation on reservationDAO.save(reservation) it will also try to persist the associated Product object. But this object is not attached to this session. So the error occur.

Leave a Comment