Interface is contract between two module/program so that they can communicate to each other....
Example : Servlet is interface and using this interface Servlet Container and our servlet
(which we define by extending HttpServlet) are communicate to each other.
Interface is contract to expose the functionality to the user and it also hides detail implementation from the user....
Other words using interface, user can access the functionality without knowing implementation and User code will not change if implementation changes.....
Examples:
jdbc programming......
RMI
EJB
abstract means you cannot see implementation......
Note :Generally first of all interface comes in the picture...
We have interface in out project to implement dao layer,service ,Facade Layer etc....
Example:-
Questions :
Where have you used generics in your project?
or
Where have you used Interface and Abstract class in your project?
Common Contract for DAO Layer
public abstract interface AbstractDao<E, I extends Serializable> {
public E findById(I id);
public void saveOrUpdate(E e);
public List<E> findByCriteria(Criterion criterion);
public List<E> findAll();
public void delete(E e);
public List<E> findByAttributeAndValue(I attribute,I value);
public List<E> getEntityListForRowNumbers(int initialRowNumber,int maximumRowNumbers);
public int getCount();
public List<E> findTopNRandomRows(int count);
List<E> findTopNRandomRowsWithAttributeAndValue(int numRows, I attribute, I value);
}
Define the Abstract class to implement common behavior for all the entities..
AbstractDaoImpl.java
/**
* @author yadna01
* @param <E>
* @param <I>
*/
public abstract class AbstractDaoImpl<E, I extends Serializable> implements
AbstractDao<E, I> {
private Class<E> entityClass;
protected AbstractDaoImpl(Class<E> entityClass) {
this.entityClass = entityClass;
}
@Autowired
@Qualifier("synegySessionFactory")
private SessionFactory psessionFactory;
public Session getCurrentSession() {
return psessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
@Override
public E findById(I id) {
return (E) getCurrentSession().get(entityClass, id);
}
@Override
public void saveOrUpdate(E e) {
/*System.out.println("Object to save" + e);*/
getCurrentSession().saveOrUpdate(e);
}
@Override
public List<E> findTopNRandomRows(int count) {
Criteria criteria = getCurrentSession().createCriteria(entityClass);
criteria.add(Restrictions.sqlRestriction("1=1 order by rand()"));
criteria.setMaxResults(count);
return criteria.list();
}
@Override
public void delete(E e) {
getCurrentSession().delete(e);
}
@SuppressWarnings("unchecked")
@Override
public List<E> findByCriteria(Criterion criterion) {
Criteria criteria = getCurrentSession().createCriteria(entityClass);
criteria.add(criterion);
return criteria.list();
}
public Criteria findByCriteria() {
Criteria criteria = getCurrentSession().createCriteria(entityClass);
return criteria;
}
@SuppressWarnings("unchecked")
@Override
public List<E> findAll() {
return getCurrentSession().createQuery("from " + entityClass.getName())
.list();
}
@Override
public int getCount() {
Number rowCount = (Number) getCurrentSession()
.createCriteria(entityClass.getName())
.setProjection(Projections.rowCount()).uniqueResult();
return rowCount.intValue();
}
public List<E> getEntityListForRowNumbers(int initialRowNumber,int maximumRowNumbers){
Criteria criteria = this.getCurrentSession().createCriteria(entityClass);
//criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.setFirstResult(initialRowNumber);
criteria.setMaxResults(maximumRowNumbers);
List<E> list = (List<E>) criteria.list();
return list;
}
@Override
public List<E> findByAttributeAndValue(I attribute, I value) {
@SuppressWarnings("unchecked")
List<E> list = getCurrentSession().createQuery(
"from " + entityClass.getName() + " e where e." + attribute
+ " like '" + value + "'").list();
return list;
}
@Override
public List<E> findTopNRandomRowsWithAttributeAndValue(int numRows, I attribute, I value) {
Criteria criteria = getCurrentSession().createCriteria(entityClass);
criteria.add(Restrictions.eq(attribute.toString(), value));
criteria.add(Restrictions.sqlRestriction("1=1 order by rand()"));
//criteria.add(Restrictions.sqlRestriction(attributeName + "=" + value + " order by rand()"));
criteria.setMaxResults(numRows);
List<E> output = criteria.list();
System.out.println("Number of results is " + output.size());
for(E e : output) {
System.out.println(e.toString());
}
return output;
}
}
@Repository("BankCustomerLoginHibernateDaoImpl")
@Transactional(propagation=Propagation.REQUIRED,value="transactionManager")
public class BankCustomerLoginHibernateDaoImpl extends AbstractDaoImpl<CustomerLoginDetailEntity,String> {
protected BankCustomerLoginHibernateDaoImpl() {
super(CustomerLoginDetailEntity.class);
}
//@Transactional(propagation=Propagation.REQUIRED) == annotation will not work
//for super class method since it is applied
public void save(CustomerLoginDetailEntity customerLoginDetailEntity){
super.saveOrUpdate(customerLoginDetailEntity);
}
@SuppressWarnings("unchecked")
public CustomerLoginDetailEntity findById(String id) {
CustomerLoginDetailEntity customerEntity=super.findById(id);
return customerEntity;
}
}

No comments:
Post a Comment