Handles closing a previously opened EntityManager
.
Handles closing a previously opened EntityManager
. Subclasses must
provide a concrete implementation.
The EntityManager
to close
Return the name of the configured persistence unit name
Return the name of the configured persistence unit name
Handles the actual opening of an EntityManager
.
Handles the actual opening of an EntityManager
. Subclasses must
provide a concrete implementation.
An appropriately configured EntityManager
Cleans up the current thread's EntityManager by closing it and removing the em from the thread-local storage.
Clears the persistence context.
Clears the persistence context. Changes made to any entities that have not been flushed to the database will not be persisted.
Closes the EntityManager.
Closes the EntityManager. Subclasses may override this if they desire different behavior.
Tests to see if the given entity is attached to the current EntityManager
Tests to see if the given entity is attached to the current EntityManager
The entity to check
true
if attached, false
otherwise
Creates a new ScalaQuery[A] using the given named query.
Creates a new ScalaQuery[A] using the given named query. The results of the named query should be of type A.
The named query to use
A new ScalaQuery[A] that uses the given named query
Creates a ScalaQuery representing the given named query with the given parameters set.
Creates a ScalaQuery representing the given named query with the given parameters set. This can be used if you want to use some of the extended functionality of ScalaQuery, such as pagination or using query hints. An example would be:
val query = EM.createNamedQuery[Book]("findAllBooks")
query.setFirstResult(20).setMaxResults(100)
The name of the query to execute
Zero or more pairs of (paramName,paramValue)
The created ScalaQuery[A]
Creates a new ScalaQuery[A] using the given native SQL query.
Creates a new ScalaQuery[A] using the given native SQL query. For more details on using native queries, see http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/query_native.html.
The query string to use
The name of the mapping from the result set to entities
A new ScalaQuery[A] that uses the given native SQL
Creates a new ScalaQuery[A] using the given native SQL query.
Creates a new ScalaQuery[A] using the given native SQL query. For more details on using native queries, see http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/query_native.html.
The query string to use
The class of the returned entities
A new ScalaQuery[A] that uses the given native SQL
Creates a new ScalaQuery[A] using the given native SQL query.
Creates a new ScalaQuery[A] using the given native SQL query. For more details on using native queries, see http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/query_native.html.
The query string to use
A new ScalaQuery[A] that uses the given native SQL
Creates a new ScalaQuery[A] using the given query string.
Creates a new ScalaQuery[A] using the given query string. The results of the query should be of type A. See http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/queryhql.html for an EJB-QL reference.
The query string (EJBQL)
A new ScalaQuery[A] that uses the given query string
Returns the current EntityManager instance.
Returns the current EntityManager instance. This leaves the implementor free to choose how they want to manage instances. For example, the implementor could use a ThreadLocal to allow for a singleton instance of ScalaEntityManager, or they could simply set up a val to hold the current instance.
This val should hold a reference to the factory that created this instance, so that the proper closeEM method can be called on shutdown.
This val should hold a reference to the factory that created this instance, so that the proper closeEM method can be called on shutdown.
Attempts to load a given entity based on its ID.
Attempts to load a given entity based on its ID. Returns an Option, with None indicating that no entity exists with the given ID.
The class of the entity to load
The ID of the entity to load
An Option with the loaded entity (Some) or None to indicate that an entity could not be loaded
Returns a List[A]
of the results of excuting the given query.
Returns a List[A]
of the results of excuting the given query.
Named parameters
may be provided to refine the query. Using Scala's syntax sugar for
Pairs and varargs, an example query might look like:
EM.findAll[User]("byUsername", "username" -> "fred")
The name of the query to execute
Zero or more pairs of (paramName,paramValue)
A List[A] representing the results of the query
Forces a flush of the current entity state to the database.
Forces a flush of the current entity state to the database.
Returns the underlying provider object for the EntityManager.
Returns the underlying provider object for the EntityManager. Implementation-specific.
The underlying provider
Returns the current flush mode for the EntityManager
Returns the current flush mode for the EntityManager
javax.persistence.FlushModeType
Get an instance, whose state may be lazily fetched.
Get an instance, whose state may be lazily fetched. An EntityNotFoundException will be thrown when the instance is accessed if no corresponding instance exists in the database. The exception may be thrown when getReference is called.
The class of the instance
The primary key of the instance
A lazily fetched instance
Returns the current EM's transaction.
Returns the current EM's transaction. Note that this will throw an exception if you're using JTA for transactions.
The current transaction
Returns a boolean indicating whether the EntityManager is open.
Returns a boolean indicating whether the EntityManager is open.
true
if the EntityManager is open, false
otherwise
Joins the EM to the currently open JTA transaction.
Joins the EM to the currently open JTA transaction. Note that this throws and exception if no JTA transaction exists.
Locks the given entity using the given lock mode.
Locks the given entity using the given lock mode.
The entity to lock
The mode of the lock
javax.persistence.LockModeType
Merges the given entity and returns the newly merged copy.
Merges the given entity and returns the newly merged copy.
The entity to merge
The newly merged copy
Handles a merge and flush in a single method.
Handles a merge and flush in a single method. This is useful if your merge should check for constraint violations, since otherwise the merge won't occur until the transaction closes.
The entity to merge.
A newly merged copy of the entity. The original entity should be discarded.
Returns a newly created ScalaEntityManager
.
Returns a newly created ScalaEntityManager
. This is the method
that clients would typically use to obtain their EM.
A new ScalaEntityManager
instance.
Persists the given entity.
Handles a persist and flush in a single method.
Handles a persist and flush in a single method. This is useful if your persist should check for constraint violations, since otherwise the persist won't occur until the transaction closes.
The entity to persist.
Reloads a given entity's state from the database, effectively discarding any local changes.
Reloads a given entity's state from the database, effectively discarding any local changes.
The entity to refresh
Removes the given entity.
Handles a remove and flush in a single method.
Handles a remove and flush in a single method. This is useful if your remove should check for constraint violations, since otherwise the remove won't occur until the transaction closes.
The entity to remove.
Sets the flush mode for the EntityManager.
Sets the flush mode for the EntityManager.
The desired flush mode
javax.persistence.FlushModeType
This trait can be mixed in so that an object may provide access to EntityManager instances on a thread-local basis. An example would be:
Best practice for this code is to ensure that when the thread exits it calls the cleanup method so that the EM is properly closed.