br.gov.frameworkdemoiselle.template
Class JPACrud<T,I>

java.lang.Object
  extended by br.gov.frameworkdemoiselle.template.JPACrud<T,I>
Type Parameters:
T - bean object type
I - bean id type
All Implemented Interfaces:
Crud<T,I>, Serializable

public class JPACrud<T,I>
extends Object
implements Crud<T,I>

JPA specific implementation for Crud interface.

Author:
SERPRO
See Also:
Crud, Serialized Form

Constructor Summary
JPACrud()
           
 
Method Summary
protected  javax.persistence.criteria.CriteriaQuery<T> createCriteriaQuery()
           
protected  javax.persistence.Query createQuery(String ql)
           
 void delete(I id)
          Finds an instance of this entity by it's primary ID and asks to the persistence provider to remove this entity instance from the persistence context.
 List<T> findAll()
           
protected  List<T> findByCriteriaQuery(javax.persistence.criteria.CriteriaQuery<T> criteriaQuery)
          Search CriteriaQuery integrated into the context of paging
protected  List<T> findByExample(T example)
          Retrieves a list of entities based on a single example instance of it.
protected  List<T> findByJPQL(String jpql)
          Search JPQL integrated into the context of paging
protected  Class<T> getBeanClass()
           
protected  javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder()
           
protected  javax.persistence.EntityManager getEntityManager()
           
protected  Pagination getPagination()
           
protected  void handleException(Throwable cause)
           
 T insert(T entity)
          Insert a new instance of the entity in the database.
 T load(I id)
           
 T update(T entity)
          Merge all changes made to the passed entity to a managed entity.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JPACrud

public JPACrud()
Method Detail

getBeanClass

protected Class<T> getBeanClass()

getCriteriaBuilder

protected javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder()

getEntityManager

protected javax.persistence.EntityManager getEntityManager()

getPagination

protected Pagination getPagination()

createCriteriaQuery

protected javax.persistence.criteria.CriteriaQuery<T> createCriteriaQuery()

createQuery

protected javax.persistence.Query createQuery(String ql)

handleException

protected void handleException(Throwable cause)
                        throws Throwable
Throws:
Throwable

insert

public T insert(T entity)
Insert a new instance of the entity in the database. After insertion the entity becomes "managed" as stated by the JPA specification.

Specified by:
insert in interface Crud<T,I>
Parameters:
entity - A non-detached instance of an entity. If this instance is not managed, it will be after this method returns.
Returns:
The same instance passed as argument.
Throws:
javax.persistence.EntityExistsException - if entity is an unmanaged instance and the persistence provider already has a persisted instance that matches the entity's primary key.
IllegalArgumentException - if the instance is not an entity
See Also:
EntityManager.persist(Object entity)

delete

public void delete(I id)
Finds an instance of this entity by it's primary ID and asks to the persistence provider to remove this entity instance from the persistence context.

Specified by:
delete in interface Crud<T,I>
See Also:
EntityManager.remove(Object entity)

update

public T update(T entity)
Merge all changes made to the passed entity to a managed entity. The passed instance is not modified nor becomes managed, instead the managed entity is returned by this method.

Specified by:
update in interface Crud<T,I>

load

public T load(I id)
Specified by:
load in interface Crud<T,I>

findAll

public List<T> findAll()
Specified by:
findAll in interface Crud<T,I>

findByJPQL

protected List<T> findByJPQL(String jpql)
Search JPQL integrated into the context of paging

Parameters:
jpql - - query in syntax JPQL
Returns:
a list of entities

findByCriteriaQuery

protected List<T> findByCriteriaQuery(javax.persistence.criteria.CriteriaQuery<T> criteriaQuery)
Search CriteriaQuery integrated into the context of paging

Parameters:
criteriaQuery - - structure CriteriaQuery
Returns:
a list of entities

findByExample

protected List<T> findByExample(T example)
Retrieves a list of entities based on a single example instance of it.

See below a sample of its usage:

 Employee example = new Employee();
 example.setId(12345);
 return (List<Employee>) findByExample(example);
 

Parameters:
example - an entity example
Returns:
a list of entities


Copyright © 2014 SERPRO - Serviço Federal de Processamento de Dados. All Rights Reserved.