Announcement Announcement Module
Collapse
No announcement yet.
I need your experience :Generic DAO and Services Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • I need your experience :Generic DAO and Services

    Your opinion and expirience is very impotent for me. I would like to create fundament for application

    1. CREATE GenericDao

    public interface GenericDao <T, PK extends Serializable> {
    PK create(T newInstance);
    T read(PK id);
    void update(T transientObject);
    void delete(T persistentObject);
    }

    2.1 CREATE implementation of GenericDAO use HIBERNATE (HibernateDaoSupport)

    public class GenericDaoHibernateImpl <T, PK extends Serializable> extends HibernateDaoSupport
    implements GenericDao<T, PK> {
    private Class<T> type;

    public GenericDaoHibernateImpl(Class<T> type) {
    this.type = type;
    }

    public PK create(T o) {
    return (PK) getSession().save(o);
    }

    public T read(PK id) {
    return (T) getSession().get(type, id);
    }

    public void update(T o) {
    getSession().update(o);
    }

    public void delete(T o) {
    getSession().delete(o);
    }

    2.2 CREATE implementation of GenericDAO use JPA HIBERNATE (JPADaoSupport)
    public class GenericDaoJPAImpl <T, PK extends Serializable> extends JPADaoSupport
    implements GenericDao<T, PK> {
    private Class<T> type;

    public GenericDaoJPAImpl(Class<T> type) {
    this.type = type;
    }

    public PK create(T o) {
    [implementation]
    }

    public T read(PK id) {
    [implementation]
    }

    public void update(T o) {
    [implementation]
    }

    public void delete(T o) {
    [implementation]
    }

    3 create spring-dao.xml
    We create all DAO that we need For example

    <bean id="personDao" class="genericdao.impl.GenericDaoHibernateImpl">
    <constructor-arg>
    <value>genericdaotest.domain.Person</value>
    </constructor-arg>
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    4 The same for Spring Service
    public interface GenericService <T, PK extends Serializable> {
    PK create(T newInstance);
    T read(PK id);
    void update(T transientObject);
    void delete(T persistentObject);
    }

    4.1 implementation use GenericDaoHibernateImpl
    4.2 implementation use GenericDaoJPAImpl

    5 create spring-service.xml
    <bean id="personService" class="genericdao.impl.GenericServiceHibernateImpl ">
    <constructor-arg>
    <value>genericdaotest.domain.Person</value>
    </constructor-arg>
    </bean>


    It is my idea according to http://www.ibm.com/developerworks/ja...enericdao.html
    If we have 100 entities we should create 100 DAO interface and 100 DAO implementation use @Repository
    next 100 Service interface and 100 Service implementation use @Service and @Autowiring
    For creating CRUD methods for all entities should create 400 objects in JAVA

    I would like to hear your idea, your experience ?

  • #2
    Try to have a look on this example:
    http://raykrueger.blogspot.com/2007/...ernatedao.html

    Comment


    • #3
      Are you really sure, that you need a DAO for every entity of yours? Let's say, that you have a Person entity, that has a one-to-one relationship with an Address entity. Then you would only have a DAO for Person and let the JPA provider or Hibernate deal transparently with the address for you. In essence, write a DAO only for entities, which have a life-cycle of their own.
      Services should be even more aggregating and modelled after your use cases, not your entities.

      HTH

      Comment


      • #4
        Not to answer your question, but an observation. I can't see where you pass the primary key type into your DAOs. You might need to read through the article again.

        In my experience, most of DAOs have their own special methods. That is to say that the super DAO class only can take care of generic data access methods. You do need one DAO per domain object. On the service lay, the business service objects can be designed in different ways such as based on the clients, or based on functionality and so on, but not based on DAO.

        Comment


        • #5
          For the basic CRUD operations if you pass the class type in a constructor (as you are doing) you need to configure 100 instances of GenericDao and need to configure 100 instances of the generic service.
          so:

          100 entity java classes
          1 Dao java class
          1 Service java class
          200 beans definitios (one for every dao, one for every sevice)

          Note that the only method that need to known the class type on generic dao is read(id), so if you change to read(id, class), passing the class type on every read call, you don't need de 200 beans definitios above.

          cheers

          Comment

          Working...
          X