7 Replies Latest reply on Mar 7, 2006 6:21 PM by nholbrook

    Generics in EJB 3.0 Result List?

    dhinojosa

      I was just wondering if and when queries will return
      "generisized" results from their query? In other words can we expect something like this in the near future?

      List result = query.getResultList(Employee.class);

      where query of course is a javax.persistence.Query object.

      or maybe a

      Query q = entityManager.createQuery(Employee.class, "select...");

        • 1. Re: Generics in EJB 3.0 Result List?
          dhinojosa

          Sure would help if I use the code tag

          List result = query.getResultList(Employee.class);
          

          where query of course is a javax.persistence.Query object.

          or maybe a
          Query q = entityManager.createQuery(Employee.class, "select...");
          


          • 2. Re: Generics in EJB 3.0 Result List?
            dhinojosa

            DAMMIT! Alright one last time

            List<Employee> result = query.getResultList(Employee.class);
            


            Query<Employee> q = entityManager.createQuery(Employee.class, "select...");
            


            • 3. Re: Generics in EJB 3.0 Result List?
              nholbrook

              Why not write your own persistence class that uses a persistence manager?

              public interface PersistenceManager
              {
              EntityManager getEntityManager();

              void insert( T entity );

              void remove( T entity );

              T update( T entity );

              T findById( Class entity, Serializable id );

              List findAll( Class entity );
              }

              Now they work with Generics.

              • 4. Re: Generics in EJB 3.0 Result List?
                nholbrook

                My findAll uses the template, too but for some reason it was stripped on the post.

                • 5. Re: Generics in EJB 3.0 Result List?
                  nholbrook

                  What a pain in the butt.....

                  If somebody can tell me how to post a greater than and less than sign on here, I will be glad to post some code. All my template references get removed.

                  • 6. Re: Generics in EJB 3.0 Result List?
                    mazz

                    Wrap it in code blocks (left square bracket followed by "code" followed by right square bracket)... there's a button labled "Code" when you are editing a reply - highlight the code and click that button.

                    List<String> foo = new List<String>;
                    


                    • 7. Re: Generics in EJB 3.0 Result List?
                      nholbrook

                      Hey.... It works. How much do you trust the code of a guy who can't figure out how to post on a message board? lol

                      Anyway, I set this up so we could have a class with standard persistence mechanisms that could be used with different persistent managers. I also like the templating mechanisms.

                      public interface PersistenceManager
                      {
                       List findByNamedQuery(String queryName, Map<String, Serializable> queryParams);
                      
                       EntityManager getEntityManager();
                      
                       <T> void insert( T transientInstance );
                      
                       <T> void remove( T persistentInstance );
                      
                       <T> T update( T detachedInstance );
                      
                       <T> T findById( Class<T> transientInstance, Serializable id );
                      
                       <T> List<T> findAll( Class<T> transientInstance );
                      }
                      
                      
                      ------------------------------------------------- Abstract Implementation -----------
                      
                      public abstract class AbstractPersistenceManagerImpl
                       implements PersistenceManager
                      {
                       private static final Log log = LogFactory.getLog(PersistenceManager.class);
                      
                       public <T> void insert(T transientInstance)
                       {
                       log.debug("persisting " + transientInstance.getClass().getName() + " instance");
                       try
                       {
                       getEntityManager().persist(transientInstance);
                       log.debug("persist successful");
                       }
                       catch (RuntimeException re)
                       {
                       throw new TessRuntimeException(re);
                       }
                       }
                      
                       public <T> void remove(T persistentInstance)
                       {
                       log.debug("removing " + persistentInstance.getClass().getName() + " instance");
                       try
                       {
                       getEntityManager().remove(persistentInstance);
                       log.debug("remove successful");
                       }
                       catch (RuntimeException re)
                       {
                       throw new TessRuntimeException(re);
                       }
                       }
                      
                       public <T> T update(T detachedInstance)
                       {
                       log.debug("merging " + detachedInstance.getClass().getName() + " instance");
                       try
                       {
                       T result = getEntityManager().merge(detachedInstance);
                       log.debug("merge successful");
                       return result;
                       }
                       catch (RuntimeException re)
                       {
                       throw new TessRuntimeException(re);
                       }
                       }
                      
                       public List findByNamedQuery(String queryName, Map<String, Serializable> queryParams)
                       {
                       log.debug("running named query " + queryName);
                       try
                       {
                       Query query = getEntityManager().createNamedQuery(queryName);
                       if (queryParams != null)
                       {
                       for (String paramName : queryParams.keySet())
                       query.setParameter(paramName, queryParams.get(paramName));
                       }
                       List result = query.getResultList();
                       log.debug("merge successful");
                       return result;
                       }
                       catch (RuntimeException re)
                       {
                       throw new TessRuntimeException(re);
                       }
                       }
                      
                       public <T> T findById(Class<T> transientInstance, Serializable id)
                       {
                       try
                       {
                       log.debug("getting " + transientInstance.getClass().getName() + " instance with id: " + id);
                       T instance = getEntityManager().find(transientInstance, id);
                       log.debug("get successful");
                       return instance;
                       }
                       catch (RuntimeException re)
                       {
                       throw new TessRuntimeException(re);
                       }
                       }
                      
                       public <T> List<T> findAll(Class<T> transientInstance)
                       {
                       try
                       {
                       log.debug("getting all records for " + transientInstance.getClass().getName());
                       List<T> instance = getEntityManager().createQuery(
                       "from " + transientInstance.getSimpleName()
                       + " as instance").getResultList();
                       log.debug("get successful");
                       return instance;
                       }
                       catch (RuntimeException re)
                       {
                       throw new TessRuntimeException(re);
                       }
                       }
                      
                       public abstract EntityManager getEntityManager();
                      }
                      
                      ------------------------------------ Implementing persistence manager ----------------------------
                      
                      @Stateless
                      @Local ({PersistenceManager.class})
                      public class SecurityPersistenceManagerImpl
                       extends AbstractPersistenceManagerImpl
                       implements PersistenceManager
                      {
                       @PersistenceContext(unitName="securitymanager")
                       EntityManager entityManager;
                      
                       @Override
                       public EntityManager getEntityManager()
                       {
                       return entityManager;
                       }
                      }