-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: One DAO per aggregate root or one DAO per domain obj?
PostPosted: Mon Jul 04, 2005 2:26 pm 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
Is it better to have one DAO per aggregate root or one DAO per domain object?

Regards,

Joshua


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 2:50 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
I have a generic DAO which I use for all domain objects that don't need any special functionality. It contains methods such as getAll, getByExample, save, delete, etc. Those more complex domain objects which need specialized querying or persistance code get their own DAOs which extend from it.

It works pretty well.


Top
 Profile  
 
 Post subject: sounds interesting
PostPosted: Mon Jul 04, 2005 3:32 pm 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
sounds interesting, care to share?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 4:18 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Sure.

Code:
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Projections;

public class DataAccessObject<E extends Comparable<? super E>>
{
   protected static final Log log = LogFactory.getLog(DataAccessObject.class);

   protected Class<? extends E> entityClass;

   public DataAccessObject(Class<? extends E> _entityClass)
   {
      entityClass = _entityClass;
      HibernateUtil.beginTransaction();
   }

   public E getById(Long id)
   {
      Session session = HibernateUtil.getSession();
      @SuppressWarnings("unchecked")
      E entity = (E)session.load(entityClass, id);
      return entity;
   }
   
   public E getByExample(E example)
   {
      Session session = HibernateUtil.getSession();

      Criteria crit = session.createCriteria(entityClass)
         .add(Example.create(example));

      @SuppressWarnings("unchecked")
      E entity = (E)crit.uniqueResult();

      return entity;
   }

   public List<E> getAll()
   {
      return getAll(true, true);
   }

   public List<E> getAll(boolean cacheable, boolean sort)
   {
      Session session = HibernateUtil.getSession();

      Criteria crit = session.createCriteria(entityClass).setCacheable(cacheable);

      @SuppressWarnings("unchecked")
      List<E> entities = crit.list();
      
      if (sort)
         Collections.sort(entities);

      return Collections.unmodifiableList(entities);
   }
   
   public List<E> getAllByExample(E example)
   {
      return getAllByExample(example, false, true);
   }
   
   public List<E> getAllByExample(E example, boolean cacheable, boolean sort)
   {
      Session session = HibernateUtil.getSession();

      Criteria crit = session.createCriteria(entityClass)
         .add(Example.create(example))
         .setCacheable(cacheable);

      @SuppressWarnings("unchecked")
      List<E> entities = crit.list();
      
      if (sort)
         Collections.sort(entities);

      return Collections.unmodifiableList(entities);
   }

   public int getCount()
   {
      Session session = HibernateUtil.getSession();

      Criteria crit = session.createCriteria(entityClass)
         .setProjection(Projections.rowCount());

      return (Integer)crit.uniqueResult();
   }

   public int getCount(E entity)
   {
      Session session = HibernateUtil.getSession();

      Criteria crit = session.createCriteria(entityClass)
         .setProjection(Projections.rowCount())
         .add(Example.create(entity));

      return (Integer)crit.uniqueResult();
   }
   
   public boolean isExists(E entity)
   {
      return getCount(entity) > 0;
   }

   public void save(E entity)
   {
      if (log.isInfoEnabled())
         log.info("Persisting " + entity);

      Session session = HibernateUtil.getSession();
      session.saveOrUpdate(entity);
   }

   public void delete(E entity)
   {
      if (log.isInfoEnabled())
         log.info("Deleting " + entity);

      Session session = HibernateUtil.getSession();
      session.delete(entity);
   }

   public static void attach(Object entity)
   {
      if (entity != null)
      {
         Session session = HibernateUtil.getSession();
         
         if (!session.contains(entity))
            session.lock(entity, LockMode.NONE);
      }
   }

   public static void detach(Object entity)
   {
      if (entity != null)
      {
         Session session = HibernateUtil.getSession();
         session.flush();
         session.evict(entity);
      }
   }
}


Top
 Profile  
 
 Post subject: very interesting indeed
PostPosted: Mon Jul 04, 2005 4:27 pm 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
This looks interesting. I am not familiar with the <? extends.. markup. What is this? Could you please take a moment to explain?

Regards,

Joshua


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 4:32 pm 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
Generics, new in Java 1.5.

http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf


Top
 Profile  
 
 Post subject: Cool!
PostPosted: Mon Jul 04, 2005 4:36 pm 
Beginner
Beginner

Joined: Sun Jun 05, 2005 9:45 am
Posts: 27
Any other suggested reading?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 7:10 pm 
Regular
Regular

Joined: Wed Sep 22, 2004 8:27 am
Posts: 89
good nebob... do yuo develop webApp?
if yes have u got problem with redeploy your webApp?

Plz it 's vert important....

1-which jar use in your webApp/lib?
2-How to open session, sessionfactory and how to close them and where?
3-Do u use c3p0 o other?
4-Which container do u use? Tomcat ?


thanks for all


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 11:10 am 
Beginner
Beginner

Joined: Wed May 18, 2005 9:48 am
Posts: 31
Hi all,

when i used generics i have this error:

Code:

D:\Netbeans\intranet\src\bbdd\dao\GenericDAO.java:23: generics are not supported in -source 1.4
(try -source 1.5 to enable generics)
public class GenericDAO<E extends Comparable<? super E>>


I use Netbeans 4.1 with JDK1.5 What's Wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 11:13 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Please use a Java beginners forum if your questions are not related to Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 11:40 am 
Beginner
Beginner

Joined: Wed May 18, 2005 9:48 am
Posts: 31
Ok I see,

First of all I think GenericDAO is a class from hibernate and i had resolved problem.

Second I think you should try to think about trying to answer questions of people. This is a Free distribution but if there is a forum maybe you want that someone answer your problems. For example my post in http://forum.hibernate.org/viewtopic.php?t=944665&highlight= that noone answered me and maybe all of you know it.

Thanks for your suggestions.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.