类 TestEntityManager


  • public class TestEntityManager
    extends Object
    Alternative to 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.
    从以下版本开始:
    1.4.0
    • 构造器概要

      构造器 
      构造器说明
      TestEntityManager​(javax.persistence.EntityManagerFactory entityManagerFactory)
      Create a new TestEntityManager instance for the given EntityManagerFactory.
    • 方法概要

      所有方法 实例方法 具体方法 
      修饰符和类型方法说明
      voidclear()
      Clear the persistence context, causing all managed entities to become detached.
      voiddetach​(Object entity)
      Remove the given entity from the persistence context, causing a managed entity to become detached.
      <E> Efind​(Class<E> entityClass, Object primaryKey)
      Find by primary key.
      voidflush()
      Synchronize the persistence context to the underlying database.
      javax.persistence.EntityManagergetEntityManager()
      Return the underlying EntityManager that's actually used to perform all operations.
      ObjectgetId​(Object entity)
      Return the ID of the given entity.
      <T> TgetId​(Object entity, Class<T> idType)
      Return the ID of the given entity cast to a specific type.
      <E> Emerge​(E entity)
      Merge the state of the given entity into the current persistence context.
      <E> Epersist​(E entity)
      Make an instance managed and persistent.
      <E> EpersistAndFlush​(E entity)
      Make an instance managed and persistent then synchronize the persistence context to the underlying database.
      ObjectpersistAndGetId​(Object entity)
      Make an instance managed and persistent then return it's ID.
      <T> TpersistAndGetId​(Object entity, Class<T> idType)
      Make an instance managed and persistent then return it's ID.
      <E> EpersistFlushFind​(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> Erefresh​(E entity)
      Refresh the state of the instance from the database, overwriting changes made to the entity, if any.
      voidremove​(Object entity)
      Remove the entity instance.
    • 构造器详细资料

      • TestEntityManager

        public TestEntityManager​(javax.persistence.EntityManagerFactory entityManagerFactory)
        Create a new TestEntityManager instance for the given EntityManagerFactory.
        参数:
        entityManagerFactory - the source entity manager factory
    • 方法详细资料

      • persistAndGetId

        public Object persistAndGetId​(Object entity)
        Make an instance managed and persistent then return it's ID. Delegates to 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 entity
        返回:
        the ID of the newly persisted entity
      • persistAndGetId

        public <T> T persistAndGetId​(Object entity,
                                     Class<T> idType)
        Make an instance managed and persistent then return it's ID. Delegates to 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 type
        参数:
        entity - the source entity
        idType - the ID type
        返回:
        the ID of the newly persisted entity
      • persist

        public <E> E persist​(E entity)
        Make an instance managed and persistent. Delegates to 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 type
        参数:
        entity - the entity to persist
        返回:
        the persisted entity
      • persistFlushFind

        public <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. Delegates to 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 type
        参数:
        entity - the entity to persist
        返回:
        the entity found using the ID of the persisted entity
      • persistAndFlush

        public <E> E persistAndFlush​(E entity)
        Make an instance managed and persistent then synchronize the persistence context to the underlying database. Delegates to 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 type
        参数:
        entity - the entity to persist
        返回:
        the persisted entity
      • merge

        public <E> E merge​(E entity)
        Merge the state of the given entity into the current persistence context. Delegates to EntityManager.merge(Object)
        类型参数:
        E - the entity type
        参数:
        entity - the entity to merge
        返回:
        the merged entity
      • remove

        public void remove​(Object entity)
        Remove the entity instance. Delegates to EntityManager.remove(Object)
        参数:
        entity - the entity to remove
      • find

        public <E> E find​(Class<E> entityClass,
                          Object primaryKey)
        Find by primary key. Delegates to EntityManager.find(Class, Object).
        类型参数:
        E - the entity type
        参数:
        entityClass - the entity class
        primaryKey - the entity primary key
        返回:
        the found entity or null if the entity does not exist
        另请参阅:
        getId(Object)
      • flush

        public void flush()
        Synchronize the persistence context to the underlying database. Delegates to EntityManager.flush().
      • refresh

        public <E> E refresh​(E entity)
        Refresh the state of the instance from the database, overwriting changes made to the entity, if any. Delegates to EntityManager.refresh(Object).
        类型参数:
        E - the entity type
        参数:
        entity - the entity to refresh
        返回:
        the refreshed entity
      • clear

        public void clear()
        Clear the persistence context, causing all managed entities to become detached. Delegates to EntityManager.clear()
      • detach

        public void detach​(Object entity)
        Remove the given entity from the persistence context, causing a managed entity to become detached. Delegates to EntityManager.detach(Object).
        参数:
        entity - the entity to detach.
      • getId

        public Object getId​(Object entity)
        Return the ID of the given entity. Delegates to PersistenceUnitUtil.getIdentifier(Object).
        参数:
        entity - the source entity
        返回:
        the ID of the entity or null
        另请参阅:
        getId(Object, Class)
      • getId

        public <T> T getId​(Object entity,
                           Class<T> idType)
        Return the ID of the given entity cast to a specific type. Delegates to PersistenceUnitUtil.getIdentifier(Object).
        类型参数:
        T - the ID type
        参数:
        entity - the source entity
        idType - the expected ID type
        返回:
        the ID of the entity or null
        另请参阅:
        getId(Object)
      • getEntityManager

        public final javax.persistence.EntityManager getEntityManager()
        Return the underlying EntityManager that's actually used to perform all operations.
        返回:
        the entity manager