sk.baka.tools.jpa
Class DaoTools

java.lang.Object
  extended by sk.baka.tools.jpa.DaoTools

public class DaoTools
extends Object

Contains useful utility methods for JPA entity retrieving.

Note for integrators: all methods in the class are meant to be run in a transaction. You'll have to configure your dependency injection tool to:

Author:
Martin Vysny

Constructor Summary
DaoTools()
           
 
Method Summary
 void delete(Object bean)
          Removes given bean.
 int deleteAll(Class<?> aClass)
          Deletes all instances of given JPA bean.
 void deleteById(Class<?> clazz, Object id)
          Deletes bean with given ID.
<T> List<T>
find(Class<T> clazz, CriteriaList criteria, Paging paging, Sorting sorting)
          The ultimate find command :) Retrieves all instances of given JPA bean, sorted by a particular column, matching given criteria.
<T> List<T>
findAll(Class<T> clazz)
          Retrieves all instances of given JPA bean.
<T> List<T>
findAll(Class<T> clazz, int firstResult, int maxResults)
          Retrieves all instances of given JPA bean.
<T> List<T>
findAll(Class<T> clazz, int firstResult, int maxResults, String column, boolean asc)
          Retrieves all instances of given JPA bean, sorted by a particular column.
<T> T
findById(Class<T> clazz, Object id)
          Returns an entity with given ID.
<T> T
findSingle(Class<T> clazz)
          Retrieves a single instances of JPA bean.
 long getCount(Class<?> clazz)
          Returns a count of all instances of given JPA bean.
 long getCount(Class<?> clazz, CriteriaList criteria)
          Returns a count of instances of given JPA bean which passes given filtering criteria.
 void persist(Object bean)
          Simply invokes the EntityManager.persist(java.lang.Object) method.
 void setEntityManager(javax.persistence.EntityManager em)
          Sets the entity manager.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DaoTools

public DaoTools()
Method Detail

setEntityManager

public void setEntityManager(javax.persistence.EntityManager em)
Sets the entity manager. Must be invoked before the object is actually used.

Parameters:
em - the entity manager, must not be null.

findAll

public <T> List<T> findAll(Class<T> clazz)
Retrieves all instances of given JPA bean.

Type Parameters:
T - the JPA bean type
Parameters:
clazz - the bean class
Returns:
list of all known bean instances, never null, may be empty.

findAll

public <T> List<T> findAll(Class<T> clazz,
                           int firstResult,
                           int maxResults)
Retrieves all instances of given JPA bean.

Type Parameters:
T - the JPA bean type
Parameters:
clazz - the bean class
firstResult - the start position of the first result, numbered from 0
maxResults - the maximum number of results to retrieve.
Returns:
list of all known bean instances, never null, may be empty.

findAll

public <T> List<T> findAll(Class<T> clazz,
                           int firstResult,
                           int maxResults,
                           String column,
                           boolean asc)
Retrieves all instances of given JPA bean, sorted by a particular column.

Type Parameters:
T - the JPA bean type
Parameters:
clazz - the bean class
firstResult - the start position of the first result, numbered from 0
maxResults - the maximum number of results to retrieve.
column - sort using this column. A java bean field name, not a physical table column name.
asc - use ascending order if true, descending order if false.
Returns:
list of all known bean instances, never null, may be empty.

find

public <T> List<T> find(Class<T> clazz,
                        CriteriaList criteria,
                        Paging paging,
                        Sorting sorting)
The ultimate find command :) Retrieves all instances of given JPA bean, sorted by a particular column, matching given criteria.

Type Parameters:
T - the JPA bean type
Parameters:
clazz - the bean class
paging - defines the result paging. If null then all results are returned.
criteria - declares conditions on the result list. If null then all results are returned.
sorting - sort using given expression. If null then the sorting is undefined.
Returns:
list of all known bean instances, never null, may be empty.

findById

public <T> T findById(Class<T> clazz,
                      Object id)
Returns an entity with given ID.

Type Parameters:
T - the entity type
Parameters:
clazz - the entity class
id - the ID
Returns:
the entity instance or null if no such entity exists.

getCount

public long getCount(Class<?> clazz)
Returns a count of all instances of given JPA bean.

Parameters:
clazz - the bean class
Returns:
count of instances of given bean.

getCount

public long getCount(Class<?> clazz,
                     CriteriaList criteria)
Returns a count of instances of given JPA bean which passes given filtering criteria.

Parameters:
clazz - the bean class
criteria - the criteria. If null then no beans are filtered out.
Returns:
count of instances of given bean.

findSingle

public <T> T findSingle(Class<T> clazz)
Retrieves a single instances of JPA bean. The function fails if there is no instance at all or 2 or more instances.

Type Parameters:
T - the JPA bean type
Parameters:
clazz - the bean class
Returns:
single instance of the bean, never null.

deleteAll

public int deleteAll(Class<?> aClass)
Deletes all instances of given JPA bean.

Parameters:
aClass - the JPA bean class.
Returns:
number of DB rows deleted.

delete

public void delete(Object bean)
Removes given bean. Invokes EntityManager.remove(java.lang.Object).

Parameters:
bean - the JPA bean

persist

public void persist(Object bean)
Simply invokes the EntityManager.persist(java.lang.Object) method. Handles possibly detached entities by invoking EntityManager.merge(java.lang.Object) if required. Expects that the entity ID is accessible by invoking zero-arg getId() method.

Parameters:
bean - the bean to persist.

deleteById

public void deleteById(Class<?> clazz,
                       Object id)
Deletes bean with given ID. Does nothing if no such bean exists or if the ID is null.

Parameters:
clazz - the bean class
id - the ID.


Copyright © 2009-2010. All Rights Reserved.