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
    If you want advice then

    1) Don't crosspost here and here
    2) Use [ code][/code ] tags that way it remains readable !!!

    Before moving on read this on if you should use HibernateDaoSupport/JpaDaoSupport, it isn't recommended.

    Regarding the amount of classes, you should only create dao's for those objects that are actual retrievable by themselves, i.e. the root objects. For instance an OrderDao but not an OrderLineDao. (I suggest reading about DDD and repositories).

    Comment

    Working...
    X