5 Replies Latest reply on Jan 29, 2006 6:53 PM by bill.burke

    Help with Entity Query

    neelixx

      Background: I have a JSF application that queries for a User in the DB. If it can't find the user, it looks in Active Directory. If the User is in Active Directory, but not in the DB, it adds the user (persists), and returns it.

      The problem is, the code never gets passed the entity query. If it can't find the user, it stops and returns null. The rest of my code never gets called.

      At first, I thought it was because the query was throwing an EJBException, but surrounding the query in a try/catch clause didn't help.

      Here is the method:

      /**
       * This method integrates the DB and AD contexts for the User. It firsts checks whether
       * the user exists in the DB. If so, it returns the user. If not it then checks AD to see
       * if the user exists. If the user exists, but not in the DB, it adds it.
       * Returns a User object from the UPN supplied, provided the user exists in AD
       * @param String userPrincipleName
       * @returns User
       * @throws UserNotFoundException
       */
      
       public User getUserByUPN(String upn) {
       User user = null;
       ADUserBean adUserBean;
      
       logger.debug("getting User from username: " + upn);
      
       // Let's try to get the user from the DB
       try {
       user = (User) em.createQuery("from User u where u.username=:upn")
       .setParameter("upn",upn)
       .getSingleResult();
       } catch (EJBException e) {
       logger.debug("Could not find Entity in DB for user "+upn);
       }
      
       if (user != null) {
       logger.debug("Found " + upn + " in DB");
       // user is in DB. No need to go any further. Return the DB user.
       return user;
       } else {
       // could not find user in the DB. This may be the first time the user
       // has been used in the system. Let's check AD to find out
       logger.debug("Could not find " + upn + " in the DB. Looking at Active Directory");
       adUserBean = new ADUserBean();
       user = adUserBean.getUserByUPN(upn);
       if (user != null) {
       //user exists in AD. Let's create a user object, save it to the DB
       //and return it
       logger.debug("Found user in Active Directory with username: " + upn);
       em.persist(user);
       return user;
       }
       }
       logger.debug("Could not find " + upn +". Returning Null");
       return null;
       }
      


      You can see the em.createQuery function, and I'm only interested in a single result. In my logs, you can clearly see that it did not find any rows in the result set. But, the rest of my code does not get called.

      At the very least, the else clause of my if statement should have been called.

      Am I using the createQuery() method incorrectly? Should I be using a different method?

      Thanks!
      Aaron

        • 1. Re: Help with Entity Query
          neelixx

          My sincere apologies, as I forgot to post the logs.

          NOTE: most boilerplate exception lines skipped for brevity:

          2006-01-29 15:55:01,307 DEBUG [com.decorativeconcepts.techdesk.ejb.UserBean] getting User from username: apaxson@mycompany.com
          2006-01-29 15:55:01,307 DEBUG [org.jboss.ejb3.entity.ManagedEntityManagerFactory] GETTING NEW EntityManager
          2006-01-29 15:55:01,307 DEBUG [org.jboss.ejb3.entity.ManagedEntityManagerFactory] logLivedSession.get() was NULL
          2006-01-29 15:55:01,308 DEBUG [org.jboss.ejb3.entity.ManagedEntityManagerFactory] LONG LIVED NOT FOUND
          2006-01-29 15:55:01,310 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 4663574942965760
          2006-01-29 15:55:01,311 DEBUG [org.hibernate.impl.SessionImpl] find: from User u where u.username=:upn
          ............................
          ............................
          2006-01-29 15:55:01,456 DEBUG [org.hibernate.loader.Loader] bindNamedParameters() apaxson@newcreative.com -> upn [1]
          2006-01-29 15:55:01,461 DEBUG [org.hibernate.type.StringType] binding 'apaxson@mycompany.com' to parameter: 1
          2006-01-29 15:55:01,464 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open ResultSet (open ResultSets: 0, globally: 0)
          2006-01-29 15:55:01,476 DEBUG [org.hibernate.loader.Loader] processing result set
          2006-01-29 15:55:01,476 DEBUG [org.hibernate.loader.Loader] done processing result set (0 rows)
          .............................
          .............................
          2006-01-29 15:55:01,490 DEBUG [org.hibernate.impl.SessionImpl] closing session
          2006-01-29 15:55:01,490 DEBUG [org.hibernate.jdbc.ConnectionManager] connection already null in cleanup : no action
          2006-01-29 15:55:01,491 DEBUG [org.hibernate.transaction.CacheSynchronization] transaction after completion callback, status: 4
          2006-01-29 15:55:01,491 DEBUG [org.hibernate.jdbc.JDBCContext] after transaction completion
          2006-01-29 15:55:01,491 DEBUG [org.hibernate.impl.SessionImpl] after transaction completion
          2006-01-29 15:55:01,506 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/TechDesk].[jsp]] Servlet.service() for servlet jsp threw exception
          javax.faces.el.EvaluationException: Cannot get value for expression '#{currentUser.user.username}'
          .............................
          .............................
          Caused by: javax.ejb.EJBException: null; CausedByException is:
           No entity found for query
           at org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:46)
           at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:70)
           at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:134)
          


          As you can see, not even my other log messages get called. Does the EntityManager have a special method to use to check for null values?

          Again, thank you all!
          Aaron

          • 2. Re: Help with Entity Query
            bill.burke

             

             try {
             user = (User) em.createQuery("from User u where u.username=:upn")
             .setParameter("upn",upn)
             .getSingleResult();
             } catch (EJBException e) {
             logger.debug("Could not find Entity in DB for user "+upn);
             }
            


            Should be

             try {
             user = (User) em.createQuery("from User u where u.username=:upn")
             .setParameter("upn",upn)
             .getSingleResult();
             } catch (EntityNotFoundException e) {
             logger.debug("Could not find Entity in DB for user "+upn);
             }
            


            I'm not sure if Hibernate is throwing the correct EntityNotFoundException, so, maybe you should catch Exception instead.



            • 3. Re: Help with Entity Query
              neelixx

              Excellent. Thanks for the response, Bill!

              Unfortunately, I also tried to catch EntityNotFoundException with no luck.

              I will try with just a generic Exception catch, and will respond back.

              thanks!

              • 4. Re: Help with Entity Query
                neelixx

                The general Exception catch worked!! thanks, Bill!!

                Would this be something I should look at JIRA and create if necessary? Or is it just a "common sense" thing. ((grin))

                • 5. Re: Help with Entity Query
                  bill.burke

                  We are aware of this problem and have (failing) junit tests in place.