Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Search + Spring Boot: java.lang.IllegalStateException: No transactional EntityManager available

I'm trying to setup Hibernate Search in my Spring Boot application following this tutorial. The problem is that I'm getting an error on startup:

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

I do not understand what causes this problem.


The relevant code:

@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;
    }

}
like image 729
Stefan Falk Avatar asked Aug 13 '18 17:08

Stefan Falk


1 Answers

I managed to get this to work by Autowiring an EntityManagerFactory instead. Then creating an EntityManager with the EntityManagerFactory.

@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();
        }

    }
like image 113
Chris M Avatar answered Nov 12 '22 11:11

Chris M