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 Ingredient
s and not Product
s.
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 Product
s (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 Product
s on their own so they're completely independent of where they are used/referenced.
Related Topics
Why Is Java.Util.Observable Not an Abstract Class
Printing Even and Odd Using Two Threads in Java
Why Does Autoreconnect=True Not Seem to Work
Converting a Jfreechart Timeseries Series with Day Data to Week or Month Data
Why Does My Spring Boot App Always Shutdown Immediately After Starting
How to Deserialize the Object, If It Was Moved to Another Package or Renamed
Hash String via Sha-256 in Java
How to Include SQLite Database in Executable Jar
Resultset.Getstring(1) Throws Java.Sql.Sqlexception: Invalid Operation at Current Cursor Position
Converting String to "Character" Array in Java
Jackson - Serialization of Entities with Birectional Relationships (Avoiding Cycles)
Java Read Large Text File with 70Million Line of Text
@Transactional Method Called from Another Method Doesn't Obtain a Transaction
What Does the Registernatives() Method Do