Generic DAO pattern in Hibernate

前端 未结 1 1766
北荒
北荒 2021-02-02 04:41

While working on hibernate we are following generic Hibernate DAO pattern as mentioned in Hibernate Doc also.

So as per this we are currently maintaining two parallel hi

1条回答
  •  暖寄归人
    2021-02-02 05:03

    Umesh I will show you how we implement this functionality

    The interface

    public interface Repository {
    
        void add(INSTANCE_CLASS instance);
        void merge(INSTANCE_CLASS instance);
        void remove(PRIMARY_KEY_CLASS id);
        INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id);
        INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id, Class fetchingStrategy, Object... args);
        List findAll();
        List findAll(Class fetchingStrategy, Object... args);
        List findAll(int pageNumber, int pageSize);
        List findAll(int pageNumber, int pageSize, Class fetchingStrategy, Object... args);
        List findAllByCriteria(Criteria criteria);
        List findAllByCriteria(Criteria criteria, Class fetchingStrategy, Object... args);
        List findAllByCriteria(int pageNumber, int pageSize, Criteria criteria);
        List findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, Class fetchingStrategy, Object... args);
    
    }
    

    Because you usually will not need all of methods shown above, we create an abstract class with the purpose of being a dummy implementation

    public abstract class AbstractRepository implements Repository {
    
        public void add(INSTANCE_CLASS instance) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void merge(INSTANCE_CLASS instance) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public void remove(PRIMARY_KEY_CLASS id) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
    
        public INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public INSTANCE_CLASS findById(PRIMARY_KEY_CLASS id, Class fetchingStrategy, Object... args) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAll() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAll(Class fetchingStrategy, Object... args) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAll(int pageNumber, int pageSize) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAll(int pageNumber, int pageSize, Class fetchingStrategy, Object... args) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAllByCriteria(Criteria criteria) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAllByCriteria(Criteria criteria, Class fetchingStrategy, Object... args) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAllByCriteria(int pageNumber, int pageSize, Criteria criteria) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, Class fetchingStrategy, Object... args) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
    }
    

    Now, for instance, if you want a repository which needs only add method, you can use

    public class PersonRepository extends AbstractRepository {
    
        public void add(Person instance) {
            /**
              * person implmentatiuon goes here
              */    
        }
    
    }
    

    If other developer try to access other than add method, he or she will get UnsupportedOperationException

    Criteria is just a marker interface.

    public interface Criteria {}
    

    The purpose of some methods define a parameter Class fetchingStrategy is to match externalized named queries. This way, I avoid hand-coded string which is error-prone. This approach is used by JSR-303 bean validation, for instance, to validate groups of properties. See here

    public class Person {
        public static interface PERSON_WITH_ADDRESS {}
    }
    

    The externalize named query is shown as follows

    
    
    
        
            
        
    
    

    So when i want to retrieve all of person with address, i call

    PersonRepository respository ...
    
    List personList = repository.findAll(PERSON_WITH_ADDRESS.class);
    

    findAll can be written as

    public class PersonRepository extends AbstractRepository {
    
        List findAll(Class fetchingStrategy, Object... args) {
            if(fetchingStrategy.isAssignableFrom(PERSON_WITH_ADDRESS.class)) {
                sessionFactory.getCurrentSession()
                              .getNamedQuery(fetchingStrategy.getSimpleName())
                              .list();
            }
    
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题