伙计们,我非常想知道如何将EntityManager.getReference与多个主键一起使用,因为在更新数据时总是会出错,
错误消息有点像:
[EL Fine]:sql:Connection(1713950866)-SELECT类别,子类别,代码,CreatedBy,CreatedDateTime,IsActive,备注,UpdatedBy,UpdatedDateTime,值from master.sysParameter WHERE((((Category =?)AND(SubCategory =? ))AND(代码=?))
绑定=> [acm,acm,acm]
javax.persistence.EntityNotFoundException:找不到ID为:acm的实体
在org.eclipse.persistence.internal.jpa.EntityManagerImpl.getReference(EntityManagerImpl.java:1398)
在我的代码中,我想定义3个变量,但我只有1个(acm)。
这是我的代码示例:
public boolean updateSysParameter(MasterSysParameterEntity masterSysParameter) {
// TODO Auto-generated method stub
boolean retVal = false;
try{
MasterSysParameterEntity updateSysParameter;
em = getEntityManagerFactory();
em.getTransaction().begin();
System.out.println("masterSysParameter.getCategory() => " +masterSysParameter.getCategory());
updateSysParameter = em.getReference(MasterSysParameterEntity.class, masterSysParameter.getCategory());
updateSysParameter = em.getReference(MasterSysParameterEntity.class, masterSysParameter.getSubCategory());
updateSysParameter = em.getReference(MasterSysParameterEntity.class, masterSysParameter.getCode());
updateSysParameter = em.merge(masterSysParameter);
em.getTransaction().commit();
retVal = true;
}catch (Exception e) {
if(em != null && null != em.getTransaction())
em.getTransaction().rollback();
e.printStackTrace();
} finally {
if (em != null) {
em.close();
}
}
return retVal;
}
非常感谢您的任何建议和帮助,这确实意味着很多
最佳答案
当实体中有多个@Id
字段时,则定义一个@IdClass
on the entity for the composite id。因此,您调用em.getReference
传入第二个参数作为“ IdClass”的实例。
MyIdClass myId = new MyIdClass(...); // Pass in whatever args your IdClass needs
MyEntity ent = em.getReference(MyEntity.class, myId);