找不到查询exception的实体

我正在执行以下行:

String queString = "some query string" Query q1 = em.createNativeQuery(queString, T03CallsLog.class); T03CallsLog newCall; newCall = (T03CallsLog) q1.getSingleResult(); //this line cause the exception after the first time 

奇怪的情况。 如果我只使用一个实例执行它它可以正常工作,但是如果我与多个实例(mdb)并行执行它,则第一个实例执行时没有任何exception,其余所有都得到此错误:

 10:04:50,750 ERROR [log] ECMSDispatcherMdb.onMessage, error: No entity found for query 

什么可能导致它? 以及它是如何在第一次工作,但对于所有其他实例它不是吗?

谢谢,

射线。

错误消息通常告诉您,查询未返回任何结果。 所以getSingleResult()失败了。

如果您期望空查询结果,请考虑使用getResultList()并使用isEmpty()测试结果:

 T03CallsLog newCall = null; List results = q1.getResultList(); if (!results.isEmpty()) newCall = (T03CallsLog) results.get(0); else // is it a problem? -> log. 

如果查询未返回任何结果,则getSingleResult()将抛出getSingleResult() 。 您确定,秒MDB会通过您的查询获得任何结果吗?

过冬

hibernate中更具体的entityManager是HibernateEntityManager。 如果你而不是

 @PersistenceContext public final EntityManager em = null; 

使用更具体

 @PersistenceContext public final HibernateEntityManager em = null; 

然后你可以使用

 String queString = "some query string" Iterator q1 = em.createNativeQuery(queString, T03CallsLog.class).iterate(); T03CallsLog newCall = q1.hasNext() ? q1.next() : null;