我有一个表单,此表单需要更新我的记录,但显然没有更新,我收到以下错误消息。在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]
控制器;它从表单获取位置用户和域信息并将其保存到DB;
@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>();
&#34;与不同实体的托管映射是什么意思&#34;我研究这个错误消息,但我想没有人得到这个错误。 Hibernate-orm
答案 0 :(得分:8)
此错误消息表明Hibernate已检测到无效的管理实体&#39; - &GT; &#39;管理实体&#39;映射键!=值。
MergeContext
存储托管实体到合并实体的映射(反之亦然),我猜你有两个不同的托管实体同时加载,表示数据库中的相同记录(&#39;为什么你会得到这个错误)。 (来源:internal source code of Hibernate Core分析)
从提供的示例中很难说另一个Users
实体。所以我在这里写了一些想法:
Users
实体可以在Domains
,Positions
,UserLanguages
,UserCertificates
或UserKnowledge
类中定义。如果您使用CascadeType.ALL
/ CascadeType.PERSIST
找到它,请删除cascade
属性。
如果某个地方您正在使用集合中的Users
个实体并尝试保留它,则可能还会遇到此错误。覆盖equals()
类的hashCode()
和Users
方法,并使用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)
从会话缓存中删除对象实例。
答案 1 :(得分:0)
有时它只是Hibernate中的一个错误:
https://hibernate.atlassian.net/browse/HHH-12439?attachmentViewMode=gallery
说明
根据实体的初始化顺序 级联合并失败,并出现IllegalStateException,如: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]
...