public class

DAO

extends Object
implements DAO EntityManagerAspect
java.lang.Object
   ↳ vars.jpa.DAO
Known Direct Subclasses

Class Overview

Persistence service implementation for use in Java SE environments. Basically, it's a decorator for EntityManager Use as: DAO dao = new DAO(entityManager); dao.startTransaction(); // Call whatever dao methods you need dao.endTransaction();

Summary

Nested Classes
enum DAO.TransactionType  
Fields
protected final Logger log
Public Constructors
DAO(EntityManager entityManager)
Constructs ...
Public Methods
void close()
Closes the associated entityManger if it's open.
void commit()
void endTransaction()
Close any open transaction used on the current thread.
boolean equalInDatastore(Object thisObj, Object thatObj)
True if the to objects represent the same object in the datastore.
<T> T find(T object)
Retrieves the object from the datastore.
List findByNamedQuery(String name)
Executes a named query that does not take any parameters
List findByNamedQuery(String name, Map<String, Object> namedParameters)
Executes a named query using a map of named parameters
<T> T findByPrimaryKey(Class<T> clazz, Object primaryKey)
EntityManager getEntityManager()
boolean isPersistent(Object entity)
Checks to see if the given object is persisted in the database
void loadLazyRelations(Object entity)
Many one-to-many relations are lazy loaded in JPA.
<T> T merge(T entity)
Update an object in the database and bring it into the current transaction
void persist(Object entity)
Insert an object into the database
void remove(Object entity)
void startTransaction()
Start a database transaction.
static Map<String, Object> toParameterMap(Object... args)
The findByNamedQuery method needs a Map of parameters.
[Expand]
Inherited Methods
From class java.lang.Object
From interface vars.DAO
From interface vars.jpa.EntityManagerAspect

Fields

protected final Logger log

Public Constructors

public DAO (EntityManager entityManager)

Constructs ...

Public Methods

public void close ()

Closes the associated entityManger if it's open.

public void commit ()

public void endTransaction ()

Close any open transaction used on the current thread.

public boolean equalInDatastore (Object thisObj, Object thatObj)

True if the to objects represent the same object in the datastore. (e.g. It basically compares the primary key)

public T find (T object)

Retrieves the object from the datastore. This ignores all state changes in the provided object and returns the copy as found in the data store.

public List findByNamedQuery (String name)

Executes a named query that does not take any parameters

Parameters
name The name of the JPL query
Returns
  • A list of objects returned by the query

public List findByNamedQuery (String name, Map<String, Object> namedParameters)

Executes a named query using a map of named parameters

Parameters
name The name of the query to execute
namedParameters A Map of the 'named' parameters to assign in the query
Returns
  • A list of objects returned by the query.

public T findByPrimaryKey (Class<T> clazz, Object primaryKey)

public EntityManager getEntityManager ()

public boolean isPersistent (Object entity)

Checks to see if the given object is persisted in the database

Parameters
entity The object of interest
Returns
  • true if it's in the database. False if it is not

public void loadLazyRelations (Object entity)

Many one-to-many relations are lazy loaded in JPA. For convience, this method will load all lazy relations of an IEntity object. This method has no effect on objects that are not persistant

Parameters
entity The persistent object whos children will be loaded from the database.

public T merge (T entity)

Update an object in the database and bring it into the current transaction

Parameters
entity The entity object whos fields are being updated in the database.

public void persist (Object entity)

Insert an object into the database

Parameters
entity The entity object to persist in the database

public void remove (Object entity)

public void startTransaction ()

Start a database transaction. Also initializes the EntityManager and returns an instance of it.

public static Map<String, Object> toParameterMap (Object... args)

The findByNamedQuery method needs a Map of parameters. This method generates the map for you. For example, instead of using: Map<String, Object> map = new HashMap<String, Object>(); map.put("groupId", "vaap.annotation"); map.put("artifactId", "VARS-Histogram"); You could use Map<String, Object> map = toParameterMap("groupId", "vaap-annotation", "artifactId", "VARS-Histogram")

Parameters
args An even numbered set of args. The first value in each pair is the parameter name as a string, the 2nd value is the parameter value
Returns
  • A Map containing the key-value pairs