2016-06-11 77 views
0

我想將Hibernate Search添加到我的項目中,我編寫這樣的示例代碼來測試它。使用EntityManager配置Hibernate搜索

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> 
+0

您是否驗證過您的類路徑沒有多個版本的休眠?您正在使用何種版本的Hiberate Search&Core? – Naros

+0

我在問題 –

回答

2

您通過Spring的LocalContainerEntityManagerFactoryBean建立持久化單元,但隨後使用普通的JPA(Persistence),以獲得它。

讓你無論是需要獲得使用Spring通過依賴注入實體管理器(工廠),或者你設置的persistence.xml所以你可以通過簡單的JPA獲得。

+0

中添加了我的hibernate依賴項你有權利,我的錯誤是無知的結果。我使用了@PersistenceContext EnitityManager em;'而不是試圖創建新的持久性單元。在使用JPA/Hibernate幾個月後,我仍然覺得自信。 –