只需更换
entityManager.persist(account);
与:
entityManager.merge(account);
并允许合并级联:
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)public AccountRole getAccountRole() { return accountRole;}因为合并会这样做:
如果您的实体是新实体,则与persist()相同。但是,如果您的实体已经存在,它将对其进行更新。



