public class TestEntityManager extends Object
EntityManager for use in JPA tests. Provides a subset of
EntityManager methods that are useful for tests as well as helper methods for
common testing tasks such as persist/flush/find.| Constructor and Description |
|---|
TestEntityManager(EntityManagerFactory entityManagerFactory)
Create a new
TestEntityManager instance for the given
EntityManagerFactory. |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clear the persistence context, causing all managed entities to become detached.
|
void |
detach(Object entity)
Remove the given entity from the persistence context, causing a managed entity to
become detached.
|
<E> E |
find(Class<E> entityClass,
Object primaryKey)
Find by primary key.
|
void |
flush()
Synchronize the persistence context to the underlying database.
|
EntityManager |
getEntityManager()
Return the underlying
EntityManager that's actually used to perform all
operations. |
Object |
getId(Object entity)
Return the ID of the given entity.
|
<T> T |
getId(Object entity,
Class<T> idType)
Return the ID of the given entity cast to a specific type.
|
<E> E |
merge(E entity)
Merge the state of the given entity into the current persistence context.
|
<E> E |
persist(E entity)
Make an instance managed and persistent.
|
<E> E |
persistAndFlush(E entity)
Make an instance managed and persistent then synchronize the persistence context to
the underlying database.
|
Object |
persistAndGetId(Object entity)
Make an instance managed and persistent then return it's ID.
|
<T> T |
persistAndGetId(Object entity,
Class<T> idType)
Make an instance managed and persistent then return it's ID.
|
<E> E |
persistFlushFind(E entity)
Make an instance managed and persistent, synchronize the persistence context to the
underlying database and finally find the persisted entity by its ID.
|
<E> E |
refresh(E entity)
Refresh the state of the instance from the database, overwriting changes made to
the entity, if any.
|
void |
remove(Object entity)
Remove the entity instance.
|
public TestEntityManager(EntityManagerFactory entityManagerFactory)
TestEntityManager instance for the given
EntityManagerFactory.entityManagerFactory - the source entity manager factorypublic Object persistAndGetId(Object entity)
EntityManager.persist(Object) then getId(Object).
Helpful when setting up test data in a test:
Object entityId = this.testEntityManager.persist(new MyEntity("Spring"));
entity - the source entitypublic <T> T persistAndGetId(Object entity, Class<T> idType)
EntityManager.persist(Object) then getId(Object, Class).
Helpful when setting up test data in a test:
Long entityId = this.testEntityManager.persist(new MyEntity("Spring"), Long.class);
T - the ID typeentity - the source entityidType - the ID typepublic <E> E persist(E entity)
EntityManager.persist(Object) then returns the original source entity.
Helpful when setting up test data in a test:
MyEntity entity = this.testEntityManager.persist(new MyEntity("Spring"));
E - the entity typeentity - the entity to persistpublic <E> E persistFlushFind(E entity)
persistAndFlush(Object) then find(Class, Object) with the
entity ID.
Helpful when ensuring that entity data is actually written and read from the underlying database correctly.
E - the entity typeentity - the entity to persistpublic <E> E persistAndFlush(E entity)
EntityManager.persist(Object) then
flush() and finally returns the original source entity.
Helpful when setting up test data in a test:
MyEntity entity = this.testEntityManager.persistAndFlush(new MyEntity("Spring"));
E - the entity typeentity - the entity to persistpublic <E> E merge(E entity)
EntityManager.merge(Object)E - the entity typeentity - the entity to mergepublic void remove(Object entity)
EntityManager.remove(Object)entity - the entity to removepublic <E> E find(Class<E> entityClass, Object primaryKey)
EntityManager.find(Class, Object).E - the entity typeentityClass - the entity classprimaryKey - the entity primary keynull if the entity does not existgetId(Object)public void flush()
EntityManager.flush().public <E> E refresh(E entity)
EntityManager.refresh(Object).E - the entity typeentity - the entity to refreshpublic void clear()
EntityManager.clear()public void detach(Object entity)
EntityManager.detach(Object).entity - the entity to detach.public Object getId(Object entity)
PersistenceUnitUtil.getIdentifier(Object).entity - the source entitynullgetId(Object, Class)public <T> T getId(Object entity, Class<T> idType)
PersistenceUnitUtil.getIdentifier(Object).T - the ID typeentity - the source entityidType - the expected ID typenullgetId(Object)public final EntityManager getEntityManager()
EntityManager that's actually used to perform all
operations.Copyright © 2017 Pivotal Software, Inc.. All rights reserved.