两天来,我一直在尝试将 Hibernate Search 与 JPA 一起使用,并且所有内容都可以干净地编译并运行,没有任何明显的错误。但是,当我尝试创建初始索引时,程序运行并打印出它正在索引的每个对象(使用手动索引),然后打印“索引完成”消息。不会抛出任何错误,但主方法永远不会退出。我正在使用 jre1.7 的 Maven 项目中的 Eclipse Kepler 中运行,因此我可以通过点击红色停止按钮来终止处理。如果我这样做,然后对索引文件进行文件搜索,我找不到任何东西。如果我运行我的搜索程序,我会得到 java.lang.IllegalArgumentException: 没有指定实体类型或其任何子类被索引。
我真的很感激任何关于如何让它发挥作用的建议。我试过回到休眠搜索的 4.2 版,我得到了同样的结果。
域类是汽车。这是索引器输出:
Indexing: Make Infinity Model G35
Indexing: Make Honda Model Civic
Indexing: Make Audi Model A4
Indexing: Make Toyota Model Carolla
Indexing Completed
如果在调试器中运行,调试窗口会在程序应该退出时显示以下活动线程:
com.xyzco.search.Indexer at localhost:52231
Daemon Thread [MySQL Statement Cancellation Timer] (Running)
Thread [Hibernate Search: Index updates queue processor for index com.xyzco.search.Car-1] (Running)
Thread [Hibernate Search: IndexWriter worker executor for com.xyzco.search.Car-1] (Running)
Thread [DestroyJavaVM] (Running)
这是索引器代码:
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.search.jpa.FullTextEntityManager;
public class Indexer {
public static void main(String[] args) {
try {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("dbschema");
EntityManager em = entityManagerFactory.createEntityManager();
FullTextEntityManager ftem = org.hibernate.search.jpa.Search
.getFullTextEntityManager(em);
ftem.getTransaction().begin();
@SuppressWarnings("unchecked")
List<Car> cars = em.createQuery(
"select c from Car c").getResultList();
for (Car car : cars) {
ftem.index(car); // manually index an item instance
System.out.println("Indexing: Make " + car.getMake() + " Model " + car.getModel());
}
ftem.getTransaction().commit(); // index are written at commit time
em.close();
System.out.println("Indexing Completed");
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是汽车类代码:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
@Entity
@Indexed //Mark for indexing
public class Car {
@Id @GeneratedValue
@DocumentId //Mark id property shared by Core and Search
private Integer id;
@Field //Mark for indexing using tokenization
private String make;
@Field
private String model;
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
private String description;
@Field(index=Index.YES, analyze=Analyze.NO, store=Store.NO)
private String options;
// getters and setters left out...
}
这是persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="dbschema"> <!-- transaction-type="RESOURCE_LOCAL"> -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.xyzco.search.Car</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/referencedata"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="hibernate.search.default.directory_provider"
value="filesystem" />
<property name="hibernate.search.default.indexBase" value="/usr/lucene/indexes" />
</properties>
</persistence-unit>
</persistence>
这是 POM 的一部分(使用 Hibernate 站点上建议的配置文件)
...
<profile>
<id>jboss-public-repository</id>
...
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${sl4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- HIBERNATE SEARCH -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>4.3.0.Final</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.2</version>
</dependency>
</dependencies>