jpa - Configuring Hibernate Search with EntityManager -
i want add hibernate search project, write sample code test it.
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();
and im getting exception this.
javax.persistence.persistenceexception: no persistence provider 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)
here repository config.
@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; } }
i have created persistence unit in entitymanagerfactory()
named pu
, why i'm getting exception? repository configuration has worked before added hibernate search.
do need add persistence.xml
? there way configure javaconfig?
my dependecies
<!-- 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>
you set persistence unit via spring's localcontainerentitymanagerfactorybean
using plain jpa (persistence
) in order obtain it.
so either need obtain entity manager (factory) through dependency injection using spring, or set persistence.xml can obtain via plain jpa.
Comments
Post a Comment