No entity found for query Exception

自闭症网瘾萝莉.ら 提交于 2019-12-30 00:54:07

问题


I am executing the following lines:

  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

weird situation. if I only execute it with one instance it works fine, but if I do it parallel with more then one instance(mdb's) then the first one is executed without any exceptions, and all the rest get this error:

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

any idea what could cause it? and how it works the first time, but for all the rest of the instances it doesn't?

thanks,

ray.


回答1:


The error message usually tells you, that the query returned no result. And so getSingleResult() fails.

Consider using getResultList() and test the result with isEmpty() if you expect empty query results:

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



回答2:


If a query returns no result, a NoResultException is thrown by getSingleResult(). Are you sure, the seconds MDB, will get any results by your query?




回答3:


Hibernate

The more specific entityManager in hibernate is the HibernateEntityManager. If you instead of

@PersistenceContext
public final EntityManager em = null;

Use the more specific

@PersistenceContext
public final HibernateEntityManager em = null;

Then you could use the

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


来源:https://stackoverflow.com/questions/4848776/no-entity-found-for-query-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!