将错误视为“传递给persist的分离实体:com.technople.domain.Role;嵌套异常是org.hibernate.PersistentObjectException:传递给persist的分离实体:com.technople.domain.Role”,同时保存数据
RolePrivilegesX Code Snippets
@Entity
@Table(name="role_privileges_X")
@NamedQuery(name="RolePrivilegesX.findAll", query="SELECT r FROM RolePrivilegesX r")
public class RolePrivilegesX extends AbstractBaseEntity{
@ManyToOne
@JoinColumn(name = "role_id")
private Role role;
@ManyToOne
@JoinColumn(name = "company_id")
private Company company;
private String privileges;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
角色片段
@Entity
@Table(name = "role")
public class Role extends AbstractBaseEntity {
@OneToMany(mappedBy = "role", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
private List<RolePrivilegesX> rolePrivilegesXs;
public void setRolePrivilegesXs(List<RolePrivilegesX> rolePrivilegesXs) {
this.rolePrivilegesXs = rolePrivilegesXs;
}
public void setRecordAssignment(RecordsAssignment recordAssignment) {
this.recordAssignment = recordAssignment;
}
public RolePrivilegesX addRolePrivilegesX(RolePrivilegesX rolePrivilegesX) {
if (getRolePrivilegesXs() == null) {
setRolePrivilegesXs(new ArrayList<>());
}
getRolePrivilegesXs().add(rolePrivilegesX);
rolePrivilegesX.setRole(this);
return rolePrivilegesX;
}
public RolePrivilegesX removeRolePrivilegesX(RolePrivilegesX rolePrivilegesX) {
List<RolePrivilegesX> rolePrivilegesXs = getRolePrivilegesXs();
if (null == rolePrivilegesXs) {
rolePrivilegesXs = Lists.newArrayList();
}
rolePrivilegesXs.remove(rolePrivilegesX);
rolePrivilegesX.setRole(null);;
return rolePrivilegesX;
}
}
AbstractBaseEntity Code Snippets
@MappedSuperclass
public class AbstractBaseEntity {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
}
RoleServiceImpl代码段
@Override
@Transactional(readOnly = false)
public RoleForm saveOrUpdate(RoleForm roleForm) {
Company company = loggedInCompany();
Employee loggedInEmployee = loggedInEmployee();
Role role = null;
if (null != roleForm.getRoleId()) {
role = roleRepository.findOne(roleForm.getRoleId());
}
if (null == role) {
role = new Role();
role = assignNewRolePrivileges(roleForm, role);
} else {
List<RolePrivilegesX> rolePrivilegesXs = role.getRolePrivilegesXs();
if (null == rolePrivilegesXs || rolePrivilegesXs.isEmpty()) {
role = assignNewRolePrivileges(roleForm, role);
} else {
rolePrivilegesXs.forEach(rolePrivilegesX -> rolePrivilegesX.setDeleted(Boolean.TRUE));
RolePrivilegesX rolePrivilegesX = createNewRolePrivileges(roleForm, role);
rolePrivilegesXs.add(rolePrivilegesX);
}
}
role.setCompany(company);
role.setCreatedBy(loggedInEmployee.getName());
role.setDeleted(Boolean.FALSE);
role.setName(roleForm.getName());
role.setParent(findRole(roleForm.getReportsTo()));
Role savedRoles = roleRepository.save(role);
return new RoleForm(savedRoles);
}
Role Repository Snippet
package com.technople.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.technople.domain.Company;
import com.technople.domain.Role;
public interface RoleRepository extends JpaRepository<Role, Long>{
List<Role> findByCompanyAndDeleted(Company company, Boolean deleted);
}
我经历了很多类似的问题,但这并没有解决我的问题。请告诉我我在这方面做错了什么。
答案 0 :(得分:2)
我觉得问题很可能是级联类型,您需要使用CascadeType.MERGE
代替CascaCadeType.ALL
。
答案 1 :(得分:1)
没关系。当我在双向关系上正确设置级联类型时,我的问题得到了解决。之前我只在父级上设置了级联类型,并且在将其设置为子级后,一切都按预期工作。
由于
答案 2 :(得分:1)
您可以将级联类型设置为Persist
,而不是All
。
因为您已经使用transactional