info.joseluismartin.dao.jpa
Class JpaUtils

java.lang.Object
  extended by info.joseluismartin.dao.jpa.JpaUtils

public abstract class JpaUtils
extends Object

Utility class for dealing with JPA API

Since:
1.1
Author:
Jose Luis Martin

Constructor Summary
JpaUtils()
           
 
Method Summary
static String addOrder(String queryString, String propertyPath, boolean asc)
          Add order by clause to queryString
static
<T> void
copyCriteria(javax.persistence.criteria.CriteriaQuery<T> from, javax.persistence.criteria.CriteriaQuery<T> to)
           
static void copyCriteriaNoSelection(javax.persistence.criteria.CriteriaQuery<?> from, javax.persistence.criteria.CriteriaQuery<?> to)
          Copy Criteria without Selection
static void copyFetches(javax.persistence.criteria.Fetch<?,?> from, javax.persistence.criteria.Fetch<?,?> to)
          Copy Fetches
static void copyJoins(javax.persistence.criteria.From<?,?> from, javax.persistence.criteria.From<?,?> to)
          Copy Joins
static
<T> Long
count(javax.persistence.EntityManager em, javax.persistence.criteria.CriteriaQuery<T> criteria)
          Result count from a CriteriaQuery
static
<T> javax.persistence.criteria.CriteriaQuery<Long>
countCriteria(javax.persistence.EntityManager em, javax.persistence.criteria.CriteriaQuery<T> criteria)
          Create a row count CriteriaQuery from a CriteriaQuery
static String createCountQueryString(String queryString)
          Create a count query string from a query string
static
<T,K> javax.persistence.criteria.Join<T,K>
findJoinedType(javax.persistence.criteria.CriteriaQuery<T> query, Class<T> rootClass, Class<K> joinClass)
          Find Joined Root of type clazz
static
<T> javax.persistence.criteria.Root<T>
findRoot(javax.persistence.criteria.CriteriaQuery<?> query, Class<T> clazz)
          Find the Root with type class on CriteriaQuery Root Set
static
<T> javax.persistence.criteria.Root<T>
findRoot(javax.persistence.criteria.CriteriaQuery<T> query)
          Find Root of result type
static String getAlias(String queryString)
          Gets the alias of root entity of JQL query
static String getKeyQuery(String queryString, String name)
          Gets Query String for selecting primary keys
static
<T> String
getOrCreateAlias(javax.persistence.criteria.Selection<T> selection)
          Gets The result alias, if none set a default one and return it
static javax.persistence.criteria.Path<?> getPath(javax.persistence.criteria.Path<?> path, String propertyPath)
          Gets a Path from Path using property path
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaUtils

public JpaUtils()
Method Detail

count

public static <T> Long count(javax.persistence.EntityManager em,
                             javax.persistence.criteria.CriteriaQuery<T> criteria)
Result count from a CriteriaQuery

Parameters:
em - Entity Manager
criteria - Criteria Query to count results
Returns:
row count

countCriteria

public static <T> javax.persistence.criteria.CriteriaQuery<Long> countCriteria(javax.persistence.EntityManager em,
                                                                               javax.persistence.criteria.CriteriaQuery<T> criteria)
Create a row count CriteriaQuery from a CriteriaQuery

Parameters:
em - entity manager
criteria - source criteria
Returns:
row coutnt CriteriaQuery

getOrCreateAlias

public static <T> String getOrCreateAlias(javax.persistence.criteria.Selection<T> selection)
Gets The result alias, if none set a default one and return it

Parameters:
selection -
Returns:
root alias or generated one

findRoot

public static <T> javax.persistence.criteria.Root<T> findRoot(javax.persistence.criteria.CriteriaQuery<T> query)
Find Root of result type

Parameters:
query - criteria query
Returns:
the root of result type or null if none

findRoot

public static <T> javax.persistence.criteria.Root<T> findRoot(javax.persistence.criteria.CriteriaQuery<?> query,
                                                              Class<T> clazz)
Find the Root with type class on CriteriaQuery Root Set

Type Parameters:
T - root type
Parameters:
query - criteria query
clazz - root type
Returns:
Root of null if none

findJoinedType

public static <T,K> javax.persistence.criteria.Join<T,K> findJoinedType(javax.persistence.criteria.CriteriaQuery<T> query,
                                                                        Class<T> rootClass,
                                                                        Class<K> joinClass)
Find Joined Root of type clazz

Type Parameters:
T -
Parameters:
query - the criteria query
rootClass - the root class
joinClass - the join class
Returns:
the Join

getPath

public static javax.persistence.criteria.Path<?> getPath(javax.persistence.criteria.Path<?> path,
                                                         String propertyPath)
Gets a Path from Path using property path

Parameters:
path - the base path
propertyPath - property path String like "customer.order.price"
Returns:
a new Path for property

createCountQueryString

public static String createCountQueryString(String queryString)
Create a count query string from a query string

Parameters:
queryString - string to parse
Returns:
the count query string

getAlias

public static String getAlias(String queryString)
Gets the alias of root entity of JQL query

Parameters:
queryString - JQL query
Returns:
alias of root entity.

addOrder

public static String addOrder(String queryString,
                              String propertyPath,
                              boolean asc)
Add order by clause to queryString

Parameters:
queryString - JPL Query String
propertyPath - Order properti
asc - true if ascending
Returns:
JQL Query String with Order clause appened.

getKeyQuery

public static String getKeyQuery(String queryString,
                                 String name)
Gets Query String for selecting primary keys

Parameters:
queryString - the original query
name - primary key name
Returns:
query string

copyCriteriaNoSelection

public static void copyCriteriaNoSelection(javax.persistence.criteria.CriteriaQuery<?> from,
                                           javax.persistence.criteria.CriteriaQuery<?> to)
Copy Criteria without Selection

Parameters:
from - source Criteria
to - destination Criteria

copyCriteria

public static <T> void copyCriteria(javax.persistence.criteria.CriteriaQuery<T> from,
                                    javax.persistence.criteria.CriteriaQuery<T> to)

copyJoins

public static void copyJoins(javax.persistence.criteria.From<?,?> from,
                             javax.persistence.criteria.From<?,?> to)
Copy Joins

Parameters:
from - source Join
to - destination Join

copyFetches

public static void copyFetches(javax.persistence.criteria.Fetch<?,?> from,
                               javax.persistence.criteria.Fetch<?,?> to)
Copy Fetches

Parameters:
from - source Fetch
to - dest Fetch


Copyright © 2012 JDAL. All Rights Reserved.