我有一个 Employee 类,它与 Technology 类具有单向的多对多关系。当我将技术添加到员工并合并员工时,记录将添加到连接表中。
但是,当我尝试通过合并员工来删除技术时,更改不会反映在数据库上。
员工类
@Entity
@Table(name = "EMPLOYEES")
public class Employee implements Serializable {
@JoinTable(name = "EMPLOYEES_TECHNOLOGIES",
joinColumns =
@JoinColumn(name = "EMP_USERNAME", referencedColumnName = "USERNAME", table = "EMPLOYEES"),
inverseJoinColumns =
@JoinColumn(name = "TEC_ID", referencedColumnName = "ID", table = "TECHNOLOGIES"))
private List<Technology> technologies;
EmployeeEAOImpl.class
@Stateless(name = "EmployeeEAO")
public class EmployeeEAOImpl implements EmployeeEAO {
@PersistenceContext(unitName = "ManagedITest")
private EntityManager em;
@Override
public void update(Employee employee) {
if (employee != null) {
try {
em.merge(employee);
em.flush();
} catch (RuntimeException ex) {
em.clear();
throw ex;
}
}
}
对我来说似乎很标准。我检查了生成的查询,当我使用技术创建/更新员工时,我得到了这个:
INSERT INTO EMPLOYEES_TECHNOLOGIES (TEC_ID, EMP_USERNAME) VALUES (?, ?)
bind => [99, TEST]
但是,当我从 Employee 中删除技术并合并时,我会收到以下错误查询:
DELETE FROM EMPLOYEES_TECHNOLOGIES WHERE ((TEC_ID = ?) AND (EMP_USERNAME = ?))
bind => [99, null]
或者我得到一个例外:
Exception Description: The attribute [id] of class [be.brail.entities.Employee] is mapped to a primary key column in the database. Updates are not allowed.
这让我认为员工正在被创建,因为我没有更改 ID,因此抛出异常是因为具有该特定 ID 的员工已经存在。至少,我是这么认为的。
为什么 DELETE 查询中的用户名参数为空,我不知道。但我想:)
提前致谢。