net.craftforge.essential.example.persistence
Class CrudDao<T>

java.lang.Object
  extended by net.craftforge.essential.example.persistence.CrudDao<T>
Type Parameters:
T - The entity type

public class CrudDao<T>
extends Object

A CRUD DAO generalizes the simple create, read, update and delete methods for entities of a given type. Each operations opens, commits and closes transactions itself.

Do not use this DAO for a sequence of operations if they shall share one transaction.

Since:
09.08.11
Author:
Christian Bick

Constructor Summary
CrudDao(javax.persistence.EntityManager entityManager)
          Initializes the CRUD DAO with the entity manager to use for its operations.
 
Method Summary
 T createEntity(T entity)
          Creates an entity by making it persistent.
 void deleteEntity(Class<T> clazz, Integer id)
          Deletes an entity of the given class with the specified id by removing it from the persistence context.
 T readEntity(Class<T> clazz, Integer id)
          Reads an entity of the given class with the specified id by finding it in the the persistence context.
protected  void requireExistence(T entity)
          Requires the existence of an entity.
 T updateEntity(Integer id, T entity)
          Updates an entity by merging it into the persistence context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CrudDao

public CrudDao(javax.persistence.EntityManager entityManager)
Initializes the CRUD DAO with the entity manager to use for its operations.

Parameters:
entityManager - The entity manager
Method Detail

createEntity

public T createEntity(T entity)
               throws net.craftforge.essential.controller.ControllerException
Creates an entity by making it persistent.

Parameters:
entity - The entity to persist
Returns:
The now persistent entity
Throws:
net.craftforge.essential.controller.ControllerException - if the entity has already been persistent

updateEntity

public T updateEntity(Integer id,
                      T entity)
               throws net.craftforge.essential.controller.ControllerException
Updates an entity by merging it into the persistence context.

Parameters:
id - The entity id
entity - The entity to merge
Returns:
The merged entity
Throws:
net.craftforge.essential.controller.ControllerException - if no entity with the given id has already been persistent

deleteEntity

public void deleteEntity(Class<T> clazz,
                         Integer id)
                  throws net.craftforge.essential.controller.ControllerException
Deletes an entity of the given class with the specified id by removing it from the persistence context.

Parameters:
clazz - The entity class
id - The entity id
Throws:
net.craftforge.essential.controller.ControllerException - if no entity with the given id has already been persistent

readEntity

public T readEntity(Class<T> clazz,
                    Integer id)
             throws net.craftforge.essential.controller.ControllerException
Reads an entity of the given class with the specified id by finding it in the the persistence context.

Parameters:
clazz - The entity class
id - The entity id
Returns:
The found entity
Throws:
net.craftforge.essential.controller.ControllerException - if no entity with the given id has already been persistent

requireExistence

protected void requireExistence(T entity)
                         throws net.craftforge.essential.controller.ControllerException
Requires the existence of an entity.

Parameters:
entity - The entity
Throws:
net.craftforge.essential.controller.ControllerException - if the entity is null


Copyright © 2011. All Rights Reserved.