Skip to content
Advertisement

Tag: jpa-2.0

How to detach an entity from an EntityManager

My environment Java 7/JPA 2/Hibernate 5.1. My Scenario I’m building a Repository pattern implementation. All code is written and it all works fine when no error condition happens. However, let’s say that three entity instances are added to the repository. First and third are ok, but second lacks value for a mandatory (not null) column. A DB error will be

java.lang.IllegalStateException: Multiple representations of the same entity with @ManyToMany 3 entities

I have 3 entities with ManyToMany relationships: Role Entity: Permission Entity: Functionality Entity: I did the following: I have created 3 functionalities: Then created 2 permissions: Then created a role: I am getting the following exception: java.lang.IllegalStateException: Multiple representations of the same entity [com.persistence.entity.admin.Functionality#1] are being merged. Detached: [com.persistence.entity.admin.Functionality@4729256a]; Detached: [com.persistence.entity.admin.Functionality@56ed25db] Answer Fixed it by removing CascadeType.MERGE on Permission entity

JPA: unidirectional many-to-one and cascading delete

Say I have a unidirectional @ManyToOne relationship like the following: If I have a parent P and children C1…Cn referencing back to P, is there a clean and pretty way in JPA to automatically remove the children C1…Cn when P is removed (i.e. entityManager.remove(P))? What I’m looking for is a functionality similar to ON DELETE CASCADE in SQL. Answer Relationships

Advertisement