使用 EntityManager 配置 Hibernate 搜索

Configuring Hibernate Search with EntityManager

我想将 Hibernate Search 添加到我的项目中,我写了这样的示例代码来测试它。

 EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pu");
 EntityManager em = entityManagerFactory.createEntityManager();

 FullTextEntityManager fullTextSession = Search.getFullTextEntityManager(em);
 em.getTransaction().begin();

 QueryBuilder builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Place.class).get();

 double centerLatitude = 0d;
 double centerLongitude = 0d;
 org.apache.lucene.search.Query luceneQuery = builder
                .spatial()
                .within(100, Unit.KM)
                .ofLatitude(centerLatitude)
                .andLongitude(centerLongitude)
                .createQuery();

 javax.persistence.Query jpaQuery =
                fullTextSession.createFullTextQuery(luceneQuery, Place.class);

  List result = jpaQuery.getResultList();

 em.getTransaction().commit();
 em.close();

我遇到这样的异常。

javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    pl.project.repository.place.CustomPlaceRepositoryImpl.findAll(CustomPlaceRepositoryImpl.java:24)
    pl.project.service.place.PlaceServiceImpl.findNearest(PlaceServiceImpl.java:157)
    pl.project.webapp.HomeController.helloFacebook(HomeController.java:41)

这是我的存储库配置。

 @Configuration
 @EnableTransactionManagement
 @EnableAspectJAutoProxy(proxyTargetClass = true)
 @PropertySource("classpath:jdbc.properties")
 @EnableJpaRepositories(value = "pl.project")
 @ComponentScan(basePackages = "pl.project.repository", includeFilters =     @ComponentScan.Filter(value = Repository.class, type =  FilterType.ANNOTATION))
 public class RepositoryConfig {

    //...

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setPersistenceUnitName("pu");
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPackagesToScan("pl.project.model");
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
        entityManagerFactoryBean.setJpaProperties(jpaProperties());
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        return entityManagerFactoryBean;
    }

    @Bean
    public Properties jpaProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", hbm);
        properties.setProperty("hibernate.enable_lazy_load_no_trans", "true");
        return properties;
    }

    @Bean
    public HibernateJpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();            hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
        return hibernateJpaVendorAdapter;
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }

    @Bean
    public DriverManagerDataSource dataSource() {
        //... driver, pass, username
        return dataSource;
    }
}

我在 entityManagerFactory() 中创建了名为 pu 的持久性单元,那么为什么我会遇到异常?在我添加 Hibernate 搜索之前,此存储库配置已经有效。

我需要添加 persistence.xml 吗?有JavaConfig配置的方法吗?

我的家属

    <!-- Hibernate -->

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.0.1.Final</version>
    </dependency>


    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search-orm</artifactId>
        <version>5.5.3.Final</version>
    </dependency>

您通过 Spring 的 LocalContainerEntityManagerFactoryBean 设置了持久性单元,但随后使用普通 JPA (Persistence) 来获取它。

所以你要么需要使用Spring通过依赖注入获取实体管理器(工厂),要么你设置persistence.xml这样你就可以获得它通过普通的 JPA。