当 id(主键)大于 1 时,如何获得 EntityManager.getReference?
How can I get EntityManager.getReference when the id(primay keys) are more than 1?
伙计们,我非常想知道如何将 EntityManager.getReference 与超过 1 个主键一起使用,因为当我想更新数据时总是出错,
错误消息有点像:
[EL Fine]: sql: Connection(1713950866)--SELECT Category, SubCategory, Code, CreatedBy, CreatedDateTime, IsActive, Remarks, UpdatedBy, UpdatedDateTime, Value FROM master.sysParameter WHERE (((Category = ?) AND (SubCategory = ?)) AND (Code = ?))
bind => [acm, acm, acm]
javax.persistence.EntityNotFoundException: Could not find entity for id: acm
at 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);
伙计们,我非常想知道如何将 EntityManager.getReference 与超过 1 个主键一起使用,因为当我想更新数据时总是出错, 错误消息有点像:
[EL Fine]: sql: Connection(1713950866)--SELECT Category, SubCategory, Code, CreatedBy, CreatedDateTime, IsActive, Remarks, UpdatedBy, UpdatedDateTime, Value FROM master.sysParameter WHERE (((Category = ?) AND (SubCategory = ?)) AND (Code = ?)) bind => [acm, acm, acm] javax.persistence.EntityNotFoundException: Could not find entity for id: acm at 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);