Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Advanced QBE Pattern (Common Using Generics)

DZone's Guide to

Advanced QBE Pattern (Common Using Generics)

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

 

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}