我有员工和职能部门,银行班级
员工和职能部门具有@OneToMany
关系,并且
员工和银行也有@OneToMany
关系。
如果用户编辑表格并更改功能和/或银行
我想更新关系。但是当我改变关系时
由于列的唯一性,我得到重复条目异常,因为
员工对象作为新实体持久存在
我试图从该函数中删除该雇员,并将该雇员的函数设置为null,并获取一个新函数并将该雇员添加到该函数中 并设置新功能,但不起作用。任何想法,请
@Entity
public class Employee extends GeneratedIdEntity<Long> {
@ManyToOne(optional = false)
private Functions function;
@ManyToOne(optional = false)
private Bank bank;
@OneToMany(
mappedBy = "employee",
fetch = LAZY,
cascade = ALL,
orphanRemoval = true
)
private List<RubricValue> rubricsValues = new ArrayList<>();
@OneToMany(
mappedBy = "employee",
fetch = LAZY,
cascade = ALL,
orphanRemoval = true
)
List<EmployeeStatus> employeesStatus=new ArrayList<>();
}
@Entity
public class Functions extends GeneratedIdEntity<Long>{
@OneToMany(
mappedBy = "function",
fetch = LAZY,
cascade = ALL,
orphanRemoval = true
)
private List<Employee> employees=new ArrayList<>();
public void addEmployee(Employee employee ){
employees.add(employee);
}
public void removeEmployee(Employee employee){
employees.remove(employee);
}
}
@Entity
public class Bank extends GeneratedIdEntity<Long> {
@OneToMany(
mappedBy = "bamk",
fetch = LAZY,
cascade = ALL,
orphanRemoval = true
)
private List<Employee> employees = new ArrayList<>();
public void addEmployee(Employee employee ){
employees.add(employee);
}
public void removeEmployee(Employee employee){
employees.remove(employee);
}
}
@Stateless
public class EmployeeService extends BaseEntityService<Long, Employee> {
@Inject
FunctionService functionService;
@Inject
BankService bankService;
public void update(Employee employee, String newFunctionName, String newBankName) {
if (!employee.getBank().getName().equals(newBankName)) {
employee.getBank().removeEmployee(employee);
employee.setBank(null);
Bank newBank = bankService.getByName(newBankName);
newBank.addEmployee(employee);
employee.setBank(newBank);
}
if (!employee.getFunction().getName().equals(newFunctionName)) {
employee.getFunction().removeEmployee(employee);
employee.setFunction(null);
Functions newFunction = functionService.getByName(newFunctionName);
newFunction.addEmployee(employee);
employee.setFunction(newFunction);
}
}
}
异常堆栈跟踪原因: java.sql.SQLIntegrityConstraintViolationException:重复的条目 键为“ REGISTRATIONNUMBER”的“ dkfhks32” com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:115) 在 com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) 在 com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:960) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1116) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1066) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1396) 在 com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1051) 在 com.sun.gjc.spi.base.PreparedStatementWrapper.executeUpdate(PreparedStatementWrapper.java:127) 在sun.reflect.GeneratedMethodAccessor54.invoke(未知源)处 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)在 com.sun.gjc.spi.jdbc40.ProfiledConnectionWrapper40 $ 1.invoke(ProfiledConnectionWrapper40.java:437) 在com.sun.proxy。$ Proxy268.executeUpdate(未知源)处 org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:898)
答案 0 :(得分:0)
您不必通过先删除现有银行的引用来更改bank
的{{1}}。您可以简单地去:
employee
这将正确更新映射。更新if (!employee.getBank().getName().equals(newBankName)) {
Bank newBank = bankService.getByName(newBankName);
//You must also do an entity validation/null check here. The newBank might not be present after all.
employee.setBank(newBank);
}
的{{1}}