我想將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>
您是否驗證過您的類路徑沒有多個版本的休眠?您正在使用何種版本的Hiberate Search&Core? – Naros
我在問題 –