org.datanucleus.jpa
Class EntityManagerImpl

java.lang.Object
  extended by org.datanucleus.jpa.EntityManagerImpl
All Implemented Interfaces:
javax.persistence.EntityManager

public class EntityManagerImpl
extends java.lang.Object
implements javax.persistence.EntityManager

EntityManager implementation for JPA.

Version:
$Revision: 1.1 $

Field Summary
protected  javax.persistence.EntityManagerFactory emf
          Parent EntityManagerFactory.
protected  javax.persistence.FlushModeType flushMode
          The Flush Mode.
protected static org.datanucleus.util.Localiser LOCALISER
          Localisation utility for output messages
protected  org.datanucleus.ObjectManager om
          The underlying ObjectManager managing the persistence.
protected  javax.persistence.PersistenceContextType persistenceContextType
          Type of Persistence Context
protected  javax.jdo.PersistenceManager pm
          Underlying PersistenceManager that provides the persistence capability.
protected  javax.persistence.EntityTransaction tx
          Current Transaction (when using ResourceLocal).
 
Constructor Summary
EntityManagerImpl(javax.persistence.EntityManagerFactory emf, javax.jdo.PersistenceManagerFactory pmf, javax.persistence.PersistenceContextType contextType)
          Constructor.
 
Method Summary
 void clear()
          Clear the persistence context, causing all managed entities to become detached.
 void close()
          Close an application-managed EntityManager.
 boolean contains(java.lang.Object entity)
          Check if the instance belongs to the current persistence context.
 javax.persistence.Query createNamedQuery(java.lang.String queryName)
          Create an instance of Query for executing a named query (in JPQL or SQL).
 javax.persistence.Query createNativeQuery(java.lang.String sqlString)
          Create an instance of Query for executing an SQL statement.
 javax.persistence.Query createNativeQuery(java.lang.String sqlString, java.lang.Class resultClass)
          Create an instance of Query for executing an SQL query.
 javax.persistence.Query createNativeQuery(java.lang.String sqlString, java.lang.String resultSetMapping)
          Create an instance of Query for executing an SQL query.
 javax.persistence.Query createQuery(java.lang.String queryString)
          Create an instance of Query for executing a JPQL statement.
 java.lang.Object find(java.lang.Class entityClass, java.lang.Object primaryKey)
          Method to find an object from its primary key.
 void flush()
          Synchronize the persistence context to the underlying database.
 java.lang.Object getDelegate()
          Return the underlying provider object for the EntityManager, if available.
 javax.persistence.EntityManagerFactory getEntityManagerFactory()
          Return the entity manager factory for the entity manager.
 javax.persistence.FlushModeType getFlushMode()
          Get the flush mode that applies to all objects contained in the persistence context.
 org.datanucleus.ObjectManager getObjectManager()
          Accessor for the underlying ObjectManager providing persistence capability.
 java.lang.Object getReference(java.lang.Class entityClass, java.lang.Object primaryKey)
          Get an instance, whose state may be lazily fetched.
 javax.persistence.EntityTransaction getTransaction()
          Return the resource-level transaction object.
 boolean isOpen()
          Determine whether the EntityManager is open.
 void joinTransaction()
          Indicate to the EntityManager that a JTA transaction is active.
 void lock(java.lang.Object entity, javax.persistence.LockModeType lockMode)
          Set the lock mode for an entity object contained in the persistence context.
 java.lang.Object merge(java.lang.Object entity)
          Merge the state of the given entity into the current persistence context.
 void persist(java.lang.Object entity)
          Make an instance managed and persistent.
 void refresh(java.lang.Object entity)
          Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
 void remove(java.lang.Object entity)
          Remove the entity instance.
 void setFlushMode(javax.persistence.FlushModeType flushMode)
          Set the flush mode that applies to all objects contained in the persistence context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALISER

protected static final org.datanucleus.util.Localiser LOCALISER
Localisation utility for output messages


pm

protected javax.jdo.PersistenceManager pm
Underlying PersistenceManager that provides the persistence capability. TODO Remove this when using just ObjectManager


om

protected org.datanucleus.ObjectManager om
The underlying ObjectManager managing the persistence.


emf

protected javax.persistence.EntityManagerFactory emf
Parent EntityManagerFactory.


tx

protected javax.persistence.EntityTransaction tx
Current Transaction (when using ResourceLocal). Will be null if using JTA.


flushMode

protected javax.persistence.FlushModeType flushMode
The Flush Mode.


persistenceContextType

protected javax.persistence.PersistenceContextType persistenceContextType
Type of Persistence Context

Constructor Detail

EntityManagerImpl

public EntityManagerImpl(javax.persistence.EntityManagerFactory emf,
                         javax.jdo.PersistenceManagerFactory pmf,
                         javax.persistence.PersistenceContextType contextType)
Constructor.

Parameters:
emf - The parent EntityManagerFactory
pmf - The underlying PersistenceManagerFactory that handles persistence
contextType - The Persistence Context type
Method Detail

clear

public void clear()
Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.

Specified by:
clear in interface javax.persistence.EntityManager

isOpen

public boolean isOpen()
Determine whether the EntityManager is open.

Specified by:
isOpen in interface javax.persistence.EntityManager
Returns:
true until the EntityManager has been closed.

close

public void close()
Close an application-managed EntityManager. After the close method has been invoked, all methods on the EntityManager instance and any Query objects obtained from it will throw the IllegalStateException except for getTransaction and isOpen (which will return false). If this method is called when the EntityManager is associated with an active transaction, the persistence context remains managed until the transaction completes.

Specified by:
close in interface javax.persistence.EntityManager
Throws:
java.lang.IllegalStateException - if the EntityManager is container-managed.

getEntityManagerFactory

public javax.persistence.EntityManagerFactory getEntityManagerFactory()
Return the entity manager factory for the entity manager.

Returns:
EntityManagerFactory instance
Throws:
java.lang.IllegalStateException - if the entity manager has been closed.

contains

public boolean contains(java.lang.Object entity)
Check if the instance belongs to the current persistence context.

Specified by:
contains in interface javax.persistence.EntityManager
Parameters:
entity -
Returns:
Whether it is contained in the current context
Throws:
java.lang.IllegalArgumentException - if not an entity

find

public java.lang.Object find(java.lang.Class entityClass,
                             java.lang.Object primaryKey)
Method to find an object from its primary key.

Specified by:
find in interface javax.persistence.EntityManager
Parameters:
entityClass - The entity class
primaryKey - The PK
Returns:
the found entity instance or null if the entity does not exist
Throws:
java.lang.IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that entity's primary key

getDelegate

public java.lang.Object getDelegate()
Return the underlying provider object for the EntityManager, if available. The result of this method is implementation specific.

Specified by:
getDelegate in interface javax.persistence.EntityManager

getReference

public java.lang.Object getReference(java.lang.Class entityClass,
                                     java.lang.Object primaryKey)
Get an instance, whose state may be lazily fetched. If the requested instance does not exist in the database, the EntityNotFoundException is thrown when the instance state is first accessed. The persistence provider runtime is permitted to throw the EntityNotFoundException when getReference is called. The application should not expect that the instance state will be available upon detachment, unless it was accessed by the application while the entity manager was open.

Specified by:
getReference in interface javax.persistence.EntityManager
Parameters:
entityClass - Class of the entity
primaryKey - The PK
Returns:
the found entity instance
Throws:
java.lang.IllegalArgumentException - if the first argument does not denote an entity type or the second argument is not a valid type for that entities PK
javax.persistence.EntityNotFoundException - if the entity state cannot be accessed

lock

public void lock(java.lang.Object entity,
                 javax.persistence.LockModeType lockMode)
Set the lock mode for an entity object contained in the persistence context.

Specified by:
lock in interface javax.persistence.EntityManager
Parameters:
entity - The Entity
lockMode - Lock mode
Throws:
javax.persistence.PersistenceException - if an unsupported lock call is made
java.lang.IllegalArgumentException - if the instance is not an entity or is a detached entity
javax.persistence.TransactionRequiredException - if there is no transaction

persist

public void persist(java.lang.Object entity)
Make an instance managed and persistent.

Specified by:
persist in interface javax.persistence.EntityManager
Parameters:
entity - The Entity
Throws:
javax.persistence.EntityExistsException - if the entity already exists. (The EntityExistsException may be thrown when the persist operation is invoked, or the EntityExistsException/PersistenceException may be thrown at flush/commit time.)
java.lang.IllegalArgumentException - if not an entity
javax.persistence.TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.

merge

public java.lang.Object merge(java.lang.Object entity)
Merge the state of the given entity into the current persistence context.

Specified by:
merge in interface javax.persistence.EntityManager
Parameters:
entity - The Entity
Returns:
the instance that the state was merged to
Throws:
java.lang.IllegalArgumentException - if instance is not an entity or is a removed entity
javax.persistence.TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.

refresh

public void refresh(java.lang.Object entity)
Refresh the state of the instance from the database, overwriting changes made to the entity, if any.

Specified by:
refresh in interface javax.persistence.EntityManager
Parameters:
entity - The Entity
Throws:
java.lang.IllegalArgumentException - if not an entity or entity is not managed
javax.persistence.TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.
javax.persistence.EntityNotFoundException - if the entity no longer exists in the database

remove

public void remove(java.lang.Object entity)
Remove the entity instance.

Specified by:
remove in interface javax.persistence.EntityManager
Parameters:
entity - The Entity
Throws:
java.lang.IllegalArgumentException - if not an entity or if a detached entity
javax.persistence.TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.

flush

public void flush()
Synchronize the persistence context to the underlying database.

Specified by:
flush in interface javax.persistence.EntityManager
Throws:
javax.persistence.TransactionRequiredException - if there is no transaction
javax.persistence.PersistenceException - if the flush fails

getFlushMode

public javax.persistence.FlushModeType getFlushMode()
Get the flush mode that applies to all objects contained in the persistence context.

Specified by:
getFlushMode in interface javax.persistence.EntityManager
Returns:
flushMode

setFlushMode

public void setFlushMode(javax.persistence.FlushModeType flushMode)
Set the flush mode that applies to all objects contained in the persistence context.

Specified by:
setFlushMode in interface javax.persistence.EntityManager
Parameters:
flushMode - Mode of flush

getTransaction

public javax.persistence.EntityTransaction getTransaction()
Return the resource-level transaction object. The EntityTransaction instance may be used serially to begin and commit multiple transactions.

Specified by:
getTransaction in interface javax.persistence.EntityManager
Returns:
EntityTransaction instance
Throws:
java.lang.IllegalStateException - if invoked on a JTA EntityManager.

joinTransaction

public void joinTransaction()
Indicate to the EntityManager that a JTA transaction is active. This method should be called on a JTA application managed EntityManager that was created outside the scope of the active transaction to associate it with the current JTA transaction.

Specified by:
joinTransaction in interface javax.persistence.EntityManager
Throws:
javax.persistence.TransactionRequiredException - if there is no transaction.

createNamedQuery

public javax.persistence.Query createNamedQuery(java.lang.String queryName)
Create an instance of Query for executing a named query (in JPQL or SQL).

Specified by:
createNamedQuery in interface javax.persistence.EntityManager
Parameters:
queryName - the name of a query defined in metadata
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if a query has not been defined with the given name

createNativeQuery

public javax.persistence.Query createNativeQuery(java.lang.String sqlString)
Create an instance of Query for executing an SQL statement.

Specified by:
createNativeQuery in interface javax.persistence.EntityManager
Parameters:
sqlString - a native SQL query string
Returns:
the new query instance

createNativeQuery

public javax.persistence.Query createNativeQuery(java.lang.String sqlString,
                                                 java.lang.Class resultClass)
Create an instance of Query for executing an SQL query.

Specified by:
createNativeQuery in interface javax.persistence.EntityManager
Parameters:
sqlString - a native SQL query string
resultClass - the class of the resulting instance(s)
Returns:
the new query instance

createNativeQuery

public javax.persistence.Query createNativeQuery(java.lang.String sqlString,
                                                 java.lang.String resultSetMapping)
Create an instance of Query for executing an SQL query.

Specified by:
createNativeQuery in interface javax.persistence.EntityManager
Parameters:
sqlString - a native SQL query string
resultSetMapping - the name of the result set mapping
Returns:
the new query instance

createQuery

public javax.persistence.Query createQuery(java.lang.String queryString)
Create an instance of Query for executing a JPQL statement.

Specified by:
createQuery in interface javax.persistence.EntityManager
Parameters:
queryString - a Java Persistence query string
Returns:
the new query instance
Throws:
java.lang.IllegalArgumentException - if query string is not valid

getObjectManager

public org.datanucleus.ObjectManager getObjectManager()
Accessor for the underlying ObjectManager providing persistence capability.

Returns:
The ObjectManager providing the persistence capability


Copyright © 2009. All Rights Reserved.