我正在使用 Hibernate-search 在我的 Jboss 应用程序中搜索数据。我有 3 个 JPA 实体类,它们都扩展了 BaseEntity 类,每个都由 Lucene 索引。例如:
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
@Temporal(TemporalType.TIMESTAMP)
private Date created;
public abstract Long getId();
}
@Entity
@Table(name = "DVD")
public class Dvd extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Field
private String title;
}
@Entity
@Table(name = "BOOK")
public class Book extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Field
private String author;
}
现在我想通过通配符搜索查询来搜索 DVD 标题或图书作者,并将结果列表作为 List。这就是我到目前为止所拥有的:
public List<BaseEntity> search(String query, int firstResult, int maxResults) {
List<BaseEntity> results = null;
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
Query luceneQuery = new WildcardQuery(new Term("*", "*" + query + "*"));
FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, BaseEntity.class);
fullTextQuery.setFirstResult(firstResult);
fullTextQuery.setMaxResults(maxResults);
results = fullTextQuery.getResultList();
return results;
}
但是有了这个,我没有得到任何结果。如果不为每个实体使用 buildQueryBuilder 怎么可能让它工作?谢谢!