类 EntityManagerFactoryUtils


  • public abstract class EntityManagerFactoryUtils
    extends Object
    Helper class featuring methods for JPA EntityManager handling, allowing for reuse of EntityManager instances within transactions. Also provides support for exception translation.

    Mainly intended for internal use within the framework.

    从以下版本开始:
    2.0
    作者:
    Juergen Hoeller
    • 方法详细资料

      • findEntityManagerFactory

        public static EntityManagerFactory findEntityManagerFactory​(ListableBeanFactory beanFactory,
                                                                    String unitName)
                                                             throws NoSuchBeanDefinitionException
        Find an EntityManagerFactory with the given name in the given Spring application context (represented as ListableBeanFactory).

        The specified unit name will be matched against the configured persistence unit, provided that a discovered EntityManagerFactory implements the EntityManagerFactoryInfo interface. If not, the persistence unit name will be matched against the Spring bean name, assuming that the EntityManagerFactory bean names follow that convention.

        If no unit name has been given, this method will search for a default EntityManagerFactory through BeanFactory.getBean(Class).

        参数:
        beanFactory - the ListableBeanFactory to search
        unitName - the name of the persistence unit (may be null or empty, in which case a single bean of type EntityManagerFactory will be searched for)
        返回:
        the EntityManagerFactory
        抛出:
        NoSuchBeanDefinitionException - if there is no such EntityManagerFactory in the context
        另请参阅:
        EntityManagerFactoryInfo.getPersistenceUnitName()
      • applyTransactionTimeout

        public static void applyTransactionTimeout​(Query query,
                                                   EntityManagerFactory emf)
        Apply the current transaction timeout, if any, to the given JPA Query object.

        This method sets the JPA 2.0 query hint "javax.persistence.query.timeout" accordingly.

        参数:
        query - the JPA Query object
        emf - the JPA EntityManagerFactory that the Query was created for
      • convertJpaAccessExceptionIfPossible

        public static DataAccessException convertJpaAccessExceptionIfPossible​(RuntimeException ex)
        Convert the given runtime exception to an appropriate exception from the org.springframework.dao hierarchy. Return null if no translation is appropriate: any other exception may have resulted from user code, and should not be translated.

        The most important cases like object not found or optimistic locking failure are covered here. For more fine-granular conversion, JpaTransactionManager etc support sophisticated translation of exceptions via a JpaDialect.

        参数:
        ex - runtime exception that occurred
        返回:
        the corresponding DataAccessException instance, or null if the exception should not be translated