net.dontdrinkandroot.persistence.dao

## Class GenericJpaDao

- java.lang.Object
- - net.dontdrinkandroot.persistence.dao.GenericJpaDao

All Implemented Interfaces:
  GenericDao

Direct Known Subclasses:
  TypedJpaDao

    public class GenericJpaDao
    extends Object
    implements GenericDao

Base implementation of a `GenericDao` that uses a JPA `EntityManager`.

Author:
  Philip W. Sorst <philip@sorst.net>

### Constructor Summary

Constructors
Constructor and Description
`GenericJpaDao()`

### Method Summary

Methods
`<E extends Entity<K>,K> void` `delete(E entity, Class<E> clazz)` Deletes the given entity of the given class.
`<E extends Entity<K>,K> void` `delete(K id, Class<E> clazz)` Deletes the entity with the given id of the given class.
`protected <V> List<V>` `find(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)` Find all entities by the given `CriteriaQuery`.
`protected <V> List<V>` `find(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery, int maxResults)` Finds all entities by the given `CriteriaQuery` and limit the result set.
`protected <V> List<V>` `find(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery, int firstResult, int maxResults)` Finds all entities by the given `CriteriaQuery` and limit the result set.
`<E extends Entity<K>,K> E` `find(K id, Class<E> clazz)` Finds the entity with the given id of the given class.
`<E extends Entity<K>,K> List<E>` `findAll(Class<E> clazz)` Finds all entities of the given class.
`protected <V> V` `findFirstOrNull(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)` Finds the first entity by the given `CriteriaQuery` or null if no result was found.
`protected <V> V` `findSingle(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)` Find a single entity by the given `CriteriaQuery`, throws an Exception if there is no or more than one result.
`protected <V> V` `findSingleOrNull(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)` Finds a single entity by the given `CriteriaQuery`, throws an Exception if there is more than one result.
`<E extends Entity<K>,K> long` `getCount(Class<E> clazz)` Counts the entities of the given class.
`protected javax.persistence.criteria.CriteriaBuilder` `getCriteriaBuilder()` Gets the criteria builder of the entity manager.
`javax.persistence.EntityManager` `getEntityManager()`
`org.slf4j.Logger` `getLogger()`
`<E extends Entity<K>,K> E` `load(K id, Class<E> clazz)` Loads the entity with the given id of the given class or throws an Exception if it was not found.
`<E extends Entity<K>,K> E` `save(E entity)` Saves the given entity.
`void` `setEntityManager(javax.persistence.EntityManager entityManager)`
`void` `setLogger(org.slf4j.Logger logger)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### GenericJpaDao

    public GenericJpaDao()

### Method Detail

#### setEntityManager

    public void setEntityManager(javax.persistence.EntityManager entityManager)

#### getEntityManager

    public javax.persistence.EntityManager getEntityManager()

#### getLogger

    public org.slf4j.Logger getLogger()

#### setLogger

    public void setLogger(org.slf4j.Logger logger)

#### delete

    @Transactional(propagation=MANDATORY)
    public <E extends Entity<K>,K> void delete(E entity,
                                                    Class<E> clazz)

Description copied from interface: `GenericDao`
Deletes the given entity of the given class.

Specified by:
  `delete` in interface `GenericDao`

#### delete

    @Transactional(propagation=MANDATORY)
    public <E extends Entity<K>,K> void delete(K id,
                                                    Class<E> clazz)

Description copied from interface: `GenericDao`
Deletes the entity with the given id of the given class.

Specified by:
  `delete` in interface `GenericDao`

#### find

    @Transactional(propagation=MANDATORY,
                   readOnly=true)
    public <E extends Entity<K>,K> E find(K id,
                                               Class<E> clazz)

Description copied from interface: `GenericDao`
Finds the entity with the given id of the given class.

Specified by:
  `find` in interface `GenericDao`

#### findAll

    @Transactional(propagation=MANDATORY,
                   readOnly=true)
    public <E extends Entity<K>,K> List<E> findAll(Class<E> clazz)

Description copied from interface: `GenericDao`
Finds all entities of the given class.

Specified by:
  `findAll` in interface `GenericDao`

#### load

    @Transactional(propagation=MANDATORY,
                   readOnly=true)
    public <E extends Entity<K>,K> E load(K id,
                                               Class<E> clazz)

Description copied from interface: `GenericDao`
Loads the entity with the given id of the given class or throws an Exception if it was not found.

Specified by:
  `load` in interface `GenericDao`

#### save

    @Transactional(propagation=MANDATORY)
    public <E extends Entity<K>,K> E save(E entity)

Description copied from interface: `GenericDao`
Saves the given entity.

Specified by:
  `save` in interface `GenericDao`
Returns:
  The saved instance of the entity.

#### getCount

    @Transactional(propagation=MANDATORY,
                   readOnly=true)
    public <E extends Entity<K>,K> long getCount(Class<E> clazz)

Description copied from interface: `GenericDao`
Counts the entities of the given class.

Specified by:
  `getCount` in interface `GenericDao`

#### getCriteriaBuilder

    protected javax.persistence.criteria.CriteriaBuilder getCriteriaBuilder()

Gets the criteria builder of the entity manager.

#### find

    protected <V> List<V> find(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)

Find all entities by the given `CriteriaQuery`.

#### find

    protected <V> List<V> find(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery,
                   int maxResults)

Finds all entities by the given `CriteriaQuery` and limit the result set.

Parameters:
  `maxResults` - The maximum number of results to retrieve.

#### find

    protected <V> List<V> find(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery,
                   int firstResult,
                   int maxResults)

Finds all entities by the given `CriteriaQuery` and limit the result set.

Parameters:
  `firstResult` - Position of the first result to retrive.
  `maxResults` - The maximum number of results to retrieve.

#### findSingle

    protected <V> V findSingle(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)

Find a single entity by the given `CriteriaQuery`, throws an Exception if there is no or more than one result.

#### findSingleOrNull

    protected <V> V findSingleOrNull(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)

Finds a single entity by the given `CriteriaQuery`, throws an Exception if there is more than one result.

Returns:
  The found entity or null if there was none found.

#### findFirstOrNull

    protected <V> V findFirstOrNull(javax.persistence.criteria.CriteriaQuery<V> criteriaQuery)

Finds the first entity by the given `CriteriaQuery` or null if no result was found.

Parameters:
  `criteriaQuery` - Query to execute.
Returns:
  The found entity or null if there was none found.