Java.Lang.Illegalstateexception: Multiple Representations of the Same Entity with @Manytomany 3 Entities

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

Fixed it by removing CascadeType.MERGE on Permission entity

JPA/Hibernate Multiple representations of the same entity

Ok - with hint from Piotr Gwiazda I solved it.

Simple and naive solution is add:

if(seller != null && buyer != null){
if(seller.equals(buyer)){
document.getDetails.setSeller(document.getDetails().getBuyer());
}
}

(better answer is SpringData)

before update(). This situation has place because when two different objects are equal but they references that are different hibernate can't handle them as same object.

Multiple representations of the same entity in SpringBoot 2.0.4

you are facing this issue because you are adding the object to the list/set which is already available.while performing save operation with oneToMany mapping

Now removing CascadeType.MERGE from cascade is one solution but not a best solution because after removing MERGE from Cascade you won't be able to update the mapped object ever

If you want to perform update operation also along with save for the mapped objects then before adding mapped object to list/collection just check/search within the list for the object and if the mapped object is available within the list then perform operation on that particular object.

keep cascade = CascadeType.ALL

Role role = roleService.findByName(RolesEnum.USER.getRoleName());
Note- make sure you have overridden hashcode/equals properly
boolean b = user2.getRoles().contains(role);
if (b!=true){
user2.getRoles().add(role);
}
user2Service.save(user);

or using stream

Role r= user2.getRoles().stream().filter(oldRole->oldRole.equals(role)).findAny().orElse(null);

if(r==null) {
user2.getRoles().add(role);
}
user2Service.save(user);

Hibernate, Spring. Multiple representations of the same entity are being merged

Changed

CascadeType.MERGE

to

CascadeType.PERSIST,
CascadeType.DETACH,
CascadeType.REFRESH,
CascadeType.REMOVE

and it works

Duplicating Entities with @ManyToMany

First off, maybe call the ingredients Ingredients and not Products.

Second, the problem probably lies in the code of method parseFoodToProduct(foodList); that we cannot see, in connection with the directive

@JsonManagedReference
@ManyToMany(cascade ={ CascadeType.PERSIST, CascadeType.MERGE})
private Set<Product> foodList = new HashSet<>();

If you create new Products (xxx = new Product(...)) in parseFoodToProduct(foodList);, instead of loading them from the database, this surely will backfire.

Leave out the CascadeType, and always create/retrieve/update/store the Products on their own so they're completely independent of where they are used/referenced.



Related Topics



Leave a reply



Submit