1 Reply Latest reply on May 26, 2008 10:53 AM by jaikiran pai

    javax.persistence.Query.getSingleResult() doesn't comply wit

    Mohammad Kargar Newbie

      According to the javadoc (http://java.sun.com/javaee/5/docs/api/javax/persistence/Query.html#getSingleResult())
      getSingleResult method is supposed to throw NoResultException when there is no result.

      the hibernate version bundled with JBoss 4.0.4 complies to the spec, but the one bundled with JBoss 4.2.2 does not follow the specification and instead returns null when there is no result. Wondering if there is any quick workaround for this problem at JBoss level? Otherwise we have to change all the code which doesn't make sense.

        • 1. Re: javax.persistence.Query.getSingleResult() doesn't comply
          jaikiran pai Master

          Please post your code and any other possible details. I tried this with a sample application on my local JBoss-4.2.2 GA setup which uses :

          2008-05-26 14:49:00,654 INFO [org.hibernate.ejb.Version] Hibernate EntityManager 3.2.1.GA
          2008-05-26 14:49:00,701 INFO [org.hibernate.cfg.annotations.Version] Hibernate Annotations 3.2.1.GA
          2008-05-26 14:49:00,717 INFO [org.hibernate.cfg.Environment] Hibernate 3.2.4.sp1
          


          This piece of code:
          public void testGetSingleResult(Long userId) {
           System.out.println("Testing getSingleResult --> Should throw exception is no records found");
           System.out.println("getSingleResult returns : " + entityManager.createQuery("from User where id = " + userId).getSingleResult());


          rightly throws the following exception when i pass a non-existent user id:
          20:14:46,893 ERROR [STDERR] Caused by: javax.persistence.NoResultException: No entity found for query
          20:14:46,893 ERROR [STDERR] at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:82)
          20:14:46,893 ERROR [STDERR] at org.myapp.ejb.impl.UserManagerBean.testGetSingleResult(UserManagerBean.java:262)
          20:14:46,893 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          20:14:46,893 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
          20:14:46,893 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
          20:14:46,893 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
          20:14:46,893 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
          20:14:46,893 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
          20:14:46,893 ERROR [STDERR] at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
          20:14:46,893 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          20:14:46,893 ERROR [STDERR] at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
          20:14:46,893 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          20:14:46,893 ERROR [STDERR] at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
          20:14:46,893 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
          20:14:46,893 ERROR [STDERR] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
          


          P.S: Make sure your application does not package or does not have an older version of Hibernate jar file which might be possibly resulting in this issue.