info.joseluismartin.dao.jpa
Class JpaDao<T,PK extends Serializable>

java.lang.Object
  extended by info.joseluismartin.dao.jpa.JpaDao<T,PK>
All Implemented Interfaces:
Dao<T,PK>, PageableDataSource<T>

public class JpaDao<T,PK extends Serializable>
extends Object
implements Dao<T,PK>

Dao implementation for JPA

Since:
1.1
Author:
Jose Luis Martin
See Also:
Dao

Constructor Summary
JpaDao()
          Default Ctor, When using it, you need to set entityClass
JpaDao(Class<T> entityClass)
          Create a new JpaDao for entity class
 
Method Summary
 void delete(T entity)
          Delete an entity
 void deleteById(PK id)
          Delete an object by primary key
 boolean exists(PK id)
          Checks for existence of an object of type T using the id arg.
 List<T> findByNamedQuery(String queryName, Map<String,Object> queryParams)
          Find a list of records by using a named query
 T get(PK id)
          Find object by id
<E> E
get(PK id, Class<E> clazz)
          
 List<T> getAll()
          Get all models
<E> List<E>
getAll(Class<E> clazz)
          
 Map<String,JpaCriteriaBuilder<T>> getCriteriaBuilderMap()
           
 javax.persistence.EntityManager getEntityManager()
           
 List<Serializable> getKeys(Page<T> page)
          Gets a List with keys of page
protected  javax.persistence.TypedQuery<T> getNamedQuery(Page<T> page)
          Gets a NamedQuery from page, setup order, params and page result count.
 Page<T> getPage(Page<T> page)
          Fill and return a page of data
 QueryFinder getQueryFinder()
           
protected  String getQueryString(String name)
          Gets query string fro named query using configured QueryFinder, if it's null use EntityTypeQueryFinder as defaults (looks for anntations on entity class).
 T initialize(T entity)
          Initalize entity at default depth
 T initialize(T entity, int depth)
          Initalize entity
protected  boolean isNew(T entity)
          Test if entity is New
 T save(T entity)
          Save or update an object
 void setCriteriaBuilderMap(Map<String,JpaCriteriaBuilder<T>> criteriaBuilderMap)
           
 void setEntityManager(javax.persistence.EntityManager em)
           
 void setQueryFinder(QueryFinder queryFinder)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JpaDao

public JpaDao()
Default Ctor, When using it, you need to set entityClass


JpaDao

public JpaDao(Class<T> entityClass)
Create a new JpaDao for entity class

Parameters:
entityClass - class to map.
Method Detail

getPage

public Page<T> getPage(Page<T> page)
Fill and return a page of data

Specified by:
getPage in interface PageableDataSource<T>
Returns:
the page

getKeys

public List<Serializable> getKeys(Page<T> page)
Gets a List with keys of page

Specified by:
getKeys in interface PageableDataSource<T>
Returns:
List with keys

getNamedQuery

protected javax.persistence.TypedQuery<T> getNamedQuery(Page<T> page)
Gets a NamedQuery from page, setup order, params and page result count.

Parameters:
page - request page
Returns:
a TypedQuery from a NamedQuery

getQueryString

protected String getQueryString(String name)
Gets query string fro named query using configured QueryFinder, if it's null use EntityTypeQueryFinder as defaults (looks for anntations on entity class).

Parameters:
name - query name
Returns:
query string.

findByNamedQuery

public List<T> findByNamedQuery(String queryName,
                                Map<String,Object> queryParams)
Find a list of records by using a named query

Specified by:
findByNamedQuery in interface Dao<T,PK extends Serializable>
Parameters:
queryName - query name of the named query
queryParams - a map of the query names and the values
Returns:
a list of the records found

get

public T get(PK id)
Find object by id

Specified by:
get in interface Dao<T,PK extends Serializable>
Parameters:
id - the identifier (primary key) of the object to get
Returns:
entity or null if none

getAll

public List<T> getAll()
Get all models

Specified by:
getAll in interface Dao<T,PK extends Serializable>
Returns:
List of entities

delete

public void delete(T entity)
Delete an entity

Specified by:
delete in interface Dao<T,PK extends Serializable>
Parameters:
entity - to delete

deleteById

public void deleteById(PK id)
Delete an object by primary key

Specified by:
deleteById in interface Dao<T,PK extends Serializable>
Parameters:
id - the primary key

exists

public boolean exists(PK id)
Checks for existence of an object of type T using the id arg.

Specified by:
exists in interface Dao<T,PK extends Serializable>
Parameters:
id - the id of the entity
Returns:
- true if it exists, false if it doesn't

initialize

public T initialize(T entity)
Initalize entity at default depth

Specified by:
initialize in interface Dao<T,PK extends Serializable>
Parameters:
entity - to intialize
Returns:
intialized entity

initialize

public T initialize(T entity,
                    int depth)
Initalize entity

Specified by:
initialize in interface Dao<T,PK extends Serializable>
Parameters:
entity - to intialize
depth - to descend.
Returns:
intialized entity

save

public T save(T entity)
Save or update an object

Specified by:
save in interface Dao<T,PK extends Serializable>
Parameters:
entity - the object to save
Returns:
the persisted object

isNew

protected boolean isNew(T entity)
Test if entity is New

Parameters:
entity -
Returns:
true if entity is new, ie not detached

getCriteriaBuilderMap

public Map<String,JpaCriteriaBuilder<T>> getCriteriaBuilderMap()
Returns:
the criteriaBuilderMap

setCriteriaBuilderMap

public void setCriteriaBuilderMap(Map<String,JpaCriteriaBuilder<T>> criteriaBuilderMap)
Parameters:
criteriaBuilderMap - the criteriaBuilderMap to set

getQueryFinder

public QueryFinder getQueryFinder()
Returns:
the queryFinder

setQueryFinder

public void setQueryFinder(QueryFinder queryFinder)
Parameters:
queryFinder - the queryFinder to set

get

public <E> E get(PK id,
                 Class<E> clazz)

Specified by:
get in interface Dao<T,PK extends Serializable>

getAll

public <E> List<E> getAll(Class<E> clazz)

Specified by:
getAll in interface Dao<T,PK extends Serializable>

getEntityManager

public javax.persistence.EntityManager getEntityManager()
Returns:
the em

setEntityManager

public void setEntityManager(javax.persistence.EntityManager em)
Parameters:
em - the em to set


Copyright © 2012 JDAL. All Rights Reserved.