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;
  |     }
  | }
  | 

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3928630#3928630

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3928630


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
JBoss-user mailing list
JBoss-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to