Graduated B.s.c Information System Engineering on BGU Uneversity (2004). Co-founded few startups in the domain of socail web. Been working on large cloud based ERP application in SAP for 7 years. Currently working as a development group manager in affiliation company. Gal is a DZone MVB and is not an employee of DZone and has posted 6 posts at DZone. You can read more from them at their website. View Full User Profile

Advanced QBE Pattern (Common Using Generics)

10.01.2012
| 2319 views |
  • submit to reddit

In a previous post I introduced the QBE pattern. The pattern saves us a lot of tedious binding code.
However, one still needs to implement the same method for each Entity.

To save us the redundant work of defining the same QBE method for each entity, where only the entity is changed, we could utilize Generics.

Using Generics we will implement only one method in a base class, and each concrete QBE (on certain @Entity) will inherit from that base class.


Lets see that in action.

First, we define the interface for the base class:

public interface BaseDAO<t> {
 
    
 
    /**
     * Perform a query based on the values in the given object.
     * Each (String) attribute will be searched based on the Like comparison.
     * Comply the QueryByExample (QBE) pattern
     * @param modelEntity The Entity with values to search
     * @param propertiesToExclude Any property which the search will ignored.
     * @return Instances that answer the given parameters values (empty if none)
     */
    public List<t> getByElementsLike(T modelEntity, List<string> propertiesToExclude);  
 
    /**
     * Perform a query based on the values in the given object.
     * Each attribute will be searched based on the Equality comparison.
     * Comply the QueryByExample (QBE) pattern
     * @param modelEntity The Entity with values to search
     * @param propertiesToExclude Any property which the search will ignored
     * @return Instances that answer the given parameters values (empty if none)
     */
    public List<t> getByElementsEqual(T modelEntity, List<string> propertiesToExclude);
 
}

 Second step is to write the Base class implementation

public class BaseDaoImpl<t> implements BaseDAO<t> {
//Get DB session access using Spring 3.1 Entity Manger
     @PersistenceContext
    protected EntityManager em;
   
    protected Class<t> entityClass;
 
    
     //Initialize the entity class
     @SuppressWarnings("unchecked")
    public BaseRepositoryImpl() {
            ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
            this.entityClass = (Class<t>) genericSuperclass.getActualTypeArguments()[0];
        }
 
    protected Class<t> getEntityClass() {
        return entityClass;
    }  
   
 
    public List<t> getByElementsLike(T modelEntity,List<string> propertiesToExclude) {
 
        // get the native hibernate session
        Session session = (Session) em.getDelegate();
 
        // create an example from our model-entity, exclude all zero valued numeric properties
        Example example = Example.create(modelEntity).excludeZeroes().enableLike(MatchMode.ANYWHERE);
 
        for (String property : propertiesToExclude) {
            example.excludeProperty(property);
        }      
 
        // create criteria based on the customer example
        Criteria criteria = session.createCriteria(getEntityClass()).add(example);
 
        // perform the query      
        return criteria.list();
 
    }
 
    public List<t> getByElementsEqual(T modelEntity,List<string> propertiesToExclude) {
 
        // get the native hibernate session
        Session session = (Session) em.getDelegate();
 
        // create an example from our customer, exclude all zero valued numeric properties
        Example example = Example.create(modelEntity).excludeZeroes();
 
        for (String property : propertiesToExclude) {
            example.excludeProperty(property);
        }  
 
        // create criteria based on the customer example
        Criteria criteria = session.createCriteria(getEntityClass()).add(example);
 
        // perform the query      
        return criteria.list();
    }
 
}

Third step is to extend the base class:

@Entity
 
public class Order{
..
}
 
  
 
@Entity
public class Item{
..
}
 
  
public class OrderDaoImpl extends BaseDaoImpl<order> implements
        BaseDao<order>{
 
//No need to implement the QBE again here
 
}
 
public class ItemDaoImpl extends BaseDaoImpl<item> implements
        BaseDao<item>{
 
//No need to implement the QBE again here
 
}

Kindly let me know for any further improvements.

 

Published at DZone with permission of Gal Levinsky, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)