使用EntityManager配置Hibernate Search

时间:2016-06-11 10:24:27

标签: hibernate jpa spring-data entitymanager hibernate-search

我想将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)

这是我的Repository配置。

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

1 个答案:

答案 0 :(得分:2)

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

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