Results 1 to 5 of 5

Thread: I don't know the correctness of this Dao pattern

  1. #1
    Join Date
    Mar 2005
    Location
    BeiJing , chain
    Posts
    16

    Default I don't know the correctness of this Dao pattern

    few days ago , a friend tell me a Dao pattern , but I don't know whether it can use in the production.
    give me some suggestion please.

    1 create a BaseDao extends HibernateDaoSupport

    public class BaseDao extends HibernateDaoSupport{

    private Log log = LogFactory.getLog(getClass());

    public Session openSession() {
    return SessionFactoryUtils.getSession(getSessionFactory() , false);
    }

    public Object get(Class entityClass, Serializable id) throws DataAccessException {
    Session session = openSession();
    try {
    return session.get(entityClass, id);
    }catch (HibernateException ex) {
    throw SessionFactoryUtils.convertHibernateAccessExceptio n(ex);
    }
    }
    .........
    }

    2 create EntityDao extends BaseDao

    public interface EntityDao {

    public Object get(Class entityClass, Serializable id) throws DataAccessException;

    public Object load(Class entityClass, Serializable id) throws DataAccessException;
    ......
    }

    //Extends BaseDao and implements EntityDao interface
    public class EntityDaoImpl extends BaseDao implements EntityDao{

    }

    3 create a EntityManager and use AOP pattern

    public interface EntityManager {

    public Object get(Class entityClass, Serializable id);

    public Object load(Class entityClass, Serializable id);
    ......
    }

    public class EntityManagerImpl implements EntityManager {

    private EntityDao entityDao;

    public void setEntityDao(EntityDao entityDao) {
    this.entityDao = entityDao;
    }

    public Object get(Class entityClass, Serializable id) {
    return entityDao.get(entityClass, id);
    }

    public Object load(Class entityClass, Serializable id) {
    return entityDao.load(entityClass, id);
    }
    ......
    }

    so we implements a common hibernate entity operate engine

    4 other BusinessService extends EntityManager

    He tell me the reason is can fast create BusinessService and with few code to write.

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    1. This is exactely what HibernateDaoSupport / HibernateTemplate are for!!!
    1.1 logger
    private Log log = LogFactory.getLog(getClass());
    HibernateDaoSupport already has a logger that logs messages for current class, you do not need to use a new one.

    1.2 session initialisation
    public Session openSession() {
    return SessionFactoryUtils.getSession(getSessionFactory() , false);
    }
    This is not necessary, you need to inject the HibernateSessionFactory into your DAO using Spring IoC and then use
    Code:
    getHibernateTemplate()
    Spring will take care of Hibernate Session initialisation.

    1.3 "base" methods
    public Object get(Class entityClass, Serializable id) throws DataAccessException {
    Session session = openSession();
    try {
    return session.get(entityClass, id);
    }catch (HibernateException ex) {
    throw SessionFactoryUtils.convertHibernateAccessExceptio n(ex);
    }
    }
    .........
    All of those methods are already taken care of by HibernateTemplate. Please review the javadoc.

    2. DAO implementations
    Basically, you should not pass the Entity.class as a parameter to a DAO that manages the Entity class!!! You should instead create higher level method that manage the Entity instances using some primary keys / criteria. In some cases, you can add specific data validation into the Dao layer so as it can be reusable by higher level layers (services).

    3. idem

    4. other BusinessService extends EntityManager (idem)
    When calling a ProductManger from a web controller to get a specific product you will use
    Code:
    Product product = (Product) productManager.get(Product.class, new Long(123));
    I really prefer this version:
    Code:
    Product product = productManager.get(new Long(123));
    less verbose, more specialized and no casting!!!!

    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Mar 2005
    Location
    BeiJing , chain
    Posts
    16

    Default

    thanks for your answer

    1.1 I understand

    1.2:
    getHibernateTemplate();

    the hibernateTemplate is so simple
    for exapmle:
    public abstract List find(int fristResult,int maxResults,String queryName);
    the hibernateTemplate cannot implements it

    1.3:
    I think it is a Template pattern

    2:
    I think your meaning is :
    each bo(business object) has only a independent dao
    for example:
    BusinessObject1 use dao1
    and
    BusinessObject2 use dao2

    so in some cases, can add specific data validation into the Dao layer so as it can be reusable by higher level layers (services).

    is it ?

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    1.2:
    getHibernateTemplate();

    the hibernateTemplate is so simple
    for exapmle:
    public abstract List find(int fristResult,int maxResults,String queryName);
    the hibernateTemplate cannot implements it
    You can extend HibernateTemplate to implement it yourself or fill a request on JIRA.

    I think your meaning is :
    each bo(business object) has only a independent dao
    for example:
    BusinessObject1 use dao1
    and
    BusinessObject2 use dao2

    so in some cases, can add specific data validation into the Dao layer so as it can be reusable by higher level layers (services).

    is it ?
    Yes
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5

    Default

    Or as mentioned, extend HibernateDaoSupport, and wrap a function around
    getHibernateTemplate().executeFind(...):
    Code:
    public List find(final int firstResult, final int maxResults) {
     return getHibernateTemplate().executeFind(new HibernateCallback() {
      public Object doInHibernate(Session session) throws HibernateException,
       SQLException {
        Query query = session.createQuery(sql);
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
        return query.list();
      }
     }
    }
    Super-easy. Yeah, it might be nice if HibernateTemplate had firstResult, maxResults, but it's easy enough to implement and it would require adding a signature for every find method supported in HibernateTemplate including .findByNamedParam et al.
    [/b]

Similar Threads

  1. MethodSecurityInterceptor not working?
    By asarco in forum Security
    Replies: 14
    Last Post: Mar 31st, 2008, 09:59 AM
  2. Replies: 10
    Last Post: Aug 21st, 2007, 03:09 AM
  3. Replies: 2
    Last Post: Oct 17th, 2005, 04:29 AM
  4. after login redirects incorrectly
    By ryan.tyer in forum Security
    Replies: 1
    Last Post: Oct 10th, 2005, 05:16 PM
  5. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •