有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JPA管理的不同实体映射错误

我有一个表格,这个表格需要更新我的记录,但显然没有更新,我得到下面的错误消息。4天后处理这个异常,我决定问一个问题。如果你需要额外的信息,我可以补充一些

JSP异常

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异常

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

控制器;从表单中获取位置用户和域信息并保存到数据库

@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
@Transactional
public class UsersService {

    public void save(Users users){
        usersRepository.save(users);
    }
}

形式

<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>

用户类

@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>();

“不同实体的托管映射”的含义是什么?我研究了这个错误消息,但我想没有人会得到这个错误Hibernate-orm


共 (2) 个答案

  1. # 1 楼答案

    有时这只是冬眠中的一个错误:

    https://hibernate.atlassian.net/browse/HHH-12439?attachmentViewMode=gallery

    Description
    Depending on the initialization order of entities a cascaded merge is failing with an IllegalStateException like:

    08:26:15,502 ERROR [org.jboss.as.ejb3.invocation] (default task-1) WFLYEJB0034: 
    EJB Invocation failed on component Bean for method public abstract void 
    org.jboss.playground.BeanRemote.errorTest(java.lang.Long,java.lang.String):
    javax.ejb.EJBException: java.lang.IllegalStateException:  
    MergeContext#attempt to create managed -> managed mapping with different entities:    
    [org.jboss.playground.Frd#9]; [org.jboss.playground.Frd#9]
    

    ...

  2. # 2 楼答案

    此错误消息表示Hibernate检测到无效的“托管实体”->;'托管实体的映射,其中key!=价值观

    MergeContext存储托管实体到合并实体的映射(反之亦然),我猜您可能同时加载了两个不同的托管实体,它们代表数据库中的同一条记录(这就是为什么会出现此错误)(来源:internal source code of Hibernate Core分析)

    从提供的示例中很难说另一个Users实体在哪里。所以我在这里写了一些想法:

    • Users实体可以在DomainsPositionsUserLanguagesUserCertificatesUserKnowledge类中定义。如果在那里找到CascadeType.ALL/CascadeType.PERSIST,请删除cascade属性

    • 如果在某个地方使用集合中的Users实体并试图将其持久化,则也可能会遇到此错误。重写Users类的equals()hashCode()方法,并使用Set来确保对象的唯一性。equals()方法可能与比较ID一样简单:

      @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();
      }
      
    • 仔细查找程序中的其他Users实体,并执行session.evict(users)从会话缓存中删除对象实例