Managed JPA mapping to different object errors

I have a form, and this form should update my record, but apparently does not update, and I get below error message. Occupying this exception after 4 days, I decide to ask a question. If you need more information, I can add some.

JSP exception;

MergeContext#attempt to create managed -> managed mapping with different entities: [main.model.Users#1]; [main.model.Users#1]; nested exception is java.lang.IllegalStateException: MergeContext#attempt to create managed -> managed mapping with different entities: [main.model.Users#1]; [main.model.Users#1] 

Java Exception

 java.lang.IllegalStateException: MergeContext#attempt to create managed -> managed mapping with different entities: [main.model.Users#1]; [main.model.Users#1] 

controller; get him information about the position of the user and the domain from the form and save it in the database;

 @PostMapping("/save-personel-info") public String savePersonelInfo(@RequestParam int id, HttpServletRequest request, @ModelAttribute("Users") Users users, @ModelAttribute("Positions") Positions positions, @ModelAttribute("Domains") Domains domains, ModelMap model){ usersService.save(users); request.setAttribute("mode", "MODE_USERS"); return "redirect:/index"; } 

Service

 @Service @Transactional public class UsersService { public void save(Users users){ usersRepository.save(users); } } 

The form

 <form:form method="POST" action="/save-personel-info" modelAttribute="Users"> <tr> <form:input id="1-1-0" type="hidden" class="form-control" path="id"/> <td><form:input id="1-1-0" type="text" class="form-control" path="username" /></td> <td><form:input id="1-1-0" type="text" class="form-control" path="mail" /></td> <td> <form:select path="Positions" class="form-control"> <form:options items="${Pst}" itemValue="id" itemLabel="position_name" /> </form:select> </td> <td> <form:select path="Domains" class="form-control"> <form:options items="${Domains}" itemValue="id" itemLabel="domain_name" /> </form:select> </td> </tr> <input type="submit" value="Submit" /> </form:form> 

Custom class

 @Component @Entity public class Users { @Id @GeneratedValue(strategy = GenerationType.AUTO) public long id; public String username; public String mail; @Temporal(TemporalType.DATE) public Date enrolment_date; @ManyToOne(cascade = CascadeType.ALL) public Domains domains; @ManyToOne(cascade = CascadeType.ALL) public Positions positions; @OneToMany(targetEntity = UserLanguages.class, mappedBy = "users", cascade = CascadeType.ALL, fetch = FetchType.EAGER) public Set<UserLanguages> userlanguages = new HashSet<UserLanguages>(); @OneToMany(targetEntity = UserCertificates.class, mappedBy = "users", cascade = CascadeType.ALL, fetch = FetchType.EAGER) public Set<UserCertificates> usercertificates = new HashSet<UserCertificates>(); @OneToMany(targetEntity = UserKnowledge.class, mappedBy = "users", cascade = CascadeType.ALL, fetch = FetchType.EAGER) public Set<UserKnowledge> userknowledge = new HashSet<UserKnowledge>(); 

What is the meaning of "managed matching with different objects", I will investigate this error message, but I think no one will get this error. Hibernate-orm

+6
source share
1 answer

This error message indicates that Hibernate detected invalid managed entity mappings 'managed entity' → 'managed entity', where key! = Value.

MergeContext stores mappings of managed objects to merged objects (and vice versa), and I assume that somehow you have two different managed objects loaded at the same time that represent the same record in the database (why can you get this error ) (Source: Hibernate Core internal analysis source code

It is difficult to say from the above example where there is another Users object. Therefore, I am writing a few ideas here:

  • An object
  • Users can be defined in the Domains , Positions , UserLanguages , UserCertificates or UserKnowledge . If you find it there with CascadeType.ALL / CascadeType.PERSIST , remove the cascade property.

  • If you use Users entities in a collection and try to save it, you may also encounter this error. Override the equals() and hashCode() methods of the Users class and use Set to ensure the object is unique. The equals() method can be as simple as comparing identifiers:

     @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Users that = (Users) o; return getId() == that.getId(); } @Override public int hashCode() { return Long.valueOf(getId()).hashCode(); } 
  • Look carefully at the other Users objects in your program and do session.evict(users) to remove the instance of the object from the session cache.

+6
source

Source: https://habr.com/ru/post/1015689/


All Articles