sk.baka.tools.jpa
Class CriteriaList

java.lang.Object
  extended by sk.baka.tools.jpa.CriteriaList
All Implemented Interfaces:
Serializable

public class CriteriaList
extends Object
implements Serializable

Builds a list of criteria: a pair of column name (or a JPA expression) and the desired value. All desired criteria must hold - the AND operator is used. This object is useful in particular when creating GUI filter submit forms.

Usage example:

final CriteriaList crit = new CriteriaList().whereEqual("bean.id", 25L);
 final Query query = em.createQuery("select JpaBean bean where " + crit.toJpaQueryCondition());
 crit.feedParameters(query);
 query.getResultList();

Author:
mvy
See Also:
Serialized Form

Nested Class Summary
static class CriteriaList.Criteria
          Holds a single criteria.
 
Field Summary
 List<CriteriaList.Criteria> criteria
          The criteria list.
 List<Object> queryParameters
          The query parameters, valid after invocation of toJpaQueryCondition().
 
Constructor Summary
CriteriaList()
           
 
Method Summary
 CriteriaList add(CriteriaList.Criteria criteria)
          A convenience method to add a criteria
 CriteriaList equal(String jpaField, Serializable value)
          A convenience method to add a requirement of equality: jpaField = value
 void feedParameters(javax.persistence.Query query)
          Feeds given query with current parameter list.
 CriteriaList getWithPrefix(String prefix)
          Prefixes the JPA field expression with given prefix and returns a new Criteria list.
 CriteriaList like(String jpaField, Serializable value)
          A convenience method to add a requirement of likeness: jpaField LIKE value
 CriteriaList sameDay(String jpaField, Date date)
          Adds an "at-day" condition to this criteria list.
 String toJpaQueryCondition()
          Converts a list of JPA criteria to a JPA query WHERE clause.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

criteria

public final List<CriteriaList.Criteria> criteria
The criteria list.


queryParameters

public final List<Object> queryParameters
The query parameters, valid after invocation of toJpaQueryCondition().

Constructor Detail

CriteriaList

public CriteriaList()
Method Detail

equal

public CriteriaList equal(String jpaField,
                          Serializable value)
A convenience method to add a requirement of equality: jpaField = value

Parameters:
jpaField - the JPA field name or an expression.
value - the desired value.
Returns:
this

like

public CriteriaList like(String jpaField,
                         Serializable value)
A convenience method to add a requirement of likeness: jpaField LIKE value

Parameters:
jpaField - the JPA field name or an expression.
value - the desired value.
Returns:
this

add

public CriteriaList add(CriteriaList.Criteria criteria)
A convenience method to add a criteria

Parameters:
criteria - the criteria
Returns:
this

sameDay

public CriteriaList sameDay(String jpaField,
                            Date date)
Adds an "at-day" condition to this criteria list.

Parameters:
jpaField - the date JPA field or expression
date - filters out records from day other than this day. The time component of this date is ignored.
Returns:
this

toJpaQueryCondition

public String toJpaQueryCondition()
Converts a list of JPA criteria to a JPA query WHERE clause.

Returns:
a WHERE clause (omitting the WHERE keyword itself).

feedParameters

public void feedParameters(javax.persistence.Query query)
Feeds given query with current parameter list.

Parameters:
query - the JPA query.

getWithPrefix

public CriteriaList getWithPrefix(String prefix)
Prefixes the JPA field expression with given prefix and returns a new Criteria list.

Parameters:
prefix - the prefix to use
Returns:
new criteria with prefixed JPA column.


Copyright © 2009-2010. All Rights Reserved.