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) { } }
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."); } }