Common DAO pattern in sleep mode

While in sleep mode, we execute the general Hibernate DAO pattern as specified in the Hibernate Doc.

So, in accordance with this, we currently support two parallel hierarchies 1) for interfaces 2) for the impression

therefore, if we work on this method, even if there is no new method, besides the standard persistannce methods, we need to create a marker interface for this entiry, as well as its Implimentation.

Despite the fact that this approach and its clear separation are no problems.

my question is is there a better way / alternative way to achieve this

Thank you in advance

+3
source share
1 answer

Umesh I will show you how we implement this functionality

Interface

public interface Repository<INSTANCE_CLASS, PRIMARY_KEY_CLASS> { 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<INSTANCE_CLASS> findAll(); List<INSTANCE_CLASS> findAll(Class fetchingStrategy, Object... args); List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize); List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize, Class fetchingStrategy, Object... args); List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria); List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria, Class fetchingStrategy, Object... args); List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria); List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, Class fetchingStrategy, Object... args); } 

Since you usually don’t need all the methods shown above, we create an abstract class to create a dummy implementation

 public abstract class AbstractRepository<INSTANCE_CLASS, PRIMARY_KEY_CLASS> implements Repository<INSTANCE_CLASS, PRIMARY_KEY_CLASS> { 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<INSTANCE_CLASS> findAll() { throw new UnsupportedOperationException("Not supported yet."); } public List<INSTANCE_CLASS> findAll(Class fetchingStrategy, Object... args) { throw new UnsupportedOperationException("Not supported yet."); } public List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize) { throw new UnsupportedOperationException("Not supported yet."); } public List<INSTANCE_CLASS> findAll(int pageNumber, int pageSize, Class fetchingStrategy, Object... args) { throw new UnsupportedOperationException("Not supported yet."); } public List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria) { throw new UnsupportedOperationException("Not supported yet."); } public List<INSTANCE_CLASS> findAllByCriteria(Criteria criteria, Class fetchingStrategy, Object... args) { throw new UnsupportedOperationException("Not supported yet."); } public List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria) { throw new UnsupportedOperationException("Not supported yet."); } public List<INSTANCE_CLASS> findAllByCriteria(int pageNumber, int pageSize, Criteria criteria, Class fetchingStrategy, Object... args) { throw new UnsupportedOperationException("Not supported yet."); } } 

Now, for example, if you need a repository that should only add a method, you can use

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

If another developer tries to access a method other than the add method, he or she will receive an UnsupportedOperationException

Criteria are just a marker interface .

 public interface Criteria {} 

The purpose of some methods is determined by the Class fetchingStrategy parameter to match the externally requested named queries . Thus, I avoid manual encoding, which is error prone. This approach is used by validating the JSR-303 bean, for example, to validate property groups. See here

 public class Person { public static interface PERSON_WITH_ADDRESS {} } 

A screen request with the name is as follows

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <query name="PERSON_WITH_ADDRESS"> <![CDATA[ from Person p left join fetch p.address ]]> </query> </hibernate-mapping> 

So, when I want to get all the people with the address, I call

 PersonRepository<Person, Integer> respository ... List<Person> personList = repository.findAll(PERSON_WITH_ADDRESS.class); 

findAll can be written as

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

All Articles