Hibernate Search + Spring Boot:java.lang.IllegalStateException:没有可用的事务性EntityManager

时间:2018-08-13 17:17:36

标签: hibernate spring-boot

我正在尝试在this tutorial之后在Spring Boot应用程序中设置Hibernate Search。问题是我在启动时遇到错误:

Caused by: java.lang.IllegalStateException: No transactional EntityManager available
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:279) ~[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at com.sun.proxy.$Proxy110.unwrap(Unknown Source) ~[na:na]
    at org.hibernate.search.jpa.Search.getSession(Search.java:55) ~[hibernate-search-orm-5.9.0.Final.jar:5.9.0.Final]
    at org.hibernate.search.jpa.Search.getFullTextEntityManager(Search.java:49) ~[hibernate-search-orm-5.9.0.Final.jar:5.9.0.Final]
    at mz.api.spring.service.HibernateSearchService.initializeHibernateSearch(HibernateSearchService.java:26) ~[classes/:na]
    at mz.api.spring.config.HibernateSearchConfig.hibernateSearchService(HibernateSearchConfig.java:25) ~[classes/:na]
    at mz.api.spring.config.HibernateSearchConfig$$EnhancerBySpringCGLIB$$6594e5b4.CGLIB$hibernateSearchService$0(<generated>) ~[classes/:na]
    at mz.api.spring.config.HibernateSearchConfig$$EnhancerBySpringCGLIB$$6594e5b4$$FastClassBySpringCGLIB$$ab525f18.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at mz.api.spring.config.HibernateSearchConfig$$EnhancerBySpringCGLIB$$6594e5b4.hibernateSearchService(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    ... 24 common frames omitted

我不明白是什么原因导致了这个问题。


相关代码:

@SpringBootApplication
@EnableJpaRepositories(basePackages = "mz.api.spring.repository")
@EntityScan(basePackages = "mz.api.hibernate.model")
@EnableTransactionManagement
public class Application {

    private final static Logger LOGGER = LogManager.getLogger(Application.class);

    public static void main(String[] args) {
        LOGGER.debug("Running application ..");
        SpringApplication.run(Application.class, args);
    }

}

..

@Service 
public class HibernateSearchService {

    private final EntityManager entityManager;

    @Autowired
    public HibernateSearchService(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void initializeHibernateSearch() {

        try {
            FullTextEntityManager fullTextEntityManager = getFullTextEntityManager(this.entityManager);
            fullTextEntityManager.createIndexer().startAndWait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }    
    }

}

..

@Configuration
public class HibernateSearchConfig {

    private final EntityManager entityManager;

    @Autowired
    public HibernateSearchConfig(EntityManager entityManager) {
        this.entityManager = entityManager;
    }


    @Bean
    HibernateSearchService hibernateSearchService() {
        HibernateSearchService hibernateSearchService = new HibernateSearchService(this.entityManager);
        hibernateSearchService.initializeHibernateSearch();
        return hibernateSearchService;
    }

}

1 个答案:

答案 0 :(得分:5)

我设法通过自动装配EntityManagerFactory使其起作用。然后使用EntityManagerFactory创建一个EntityManager。

@Service
public class HibernateSearchService {

    private final EntityManager entityManager;

    @Autowired
    public HibernateSearchService(final EntityManagerFactory entityManagerFactory) {
        this.entityManager = entityManagerFactory.createEntityManager();
    }

    @PostConstruct
    public void initializeHibernateSearch() {

        try {
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
            fullTextEntityManager.createIndexer().startAndWait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }