2013-04-22 126 views
0

我正在使用Hibernate搜索在我的Jboss應用程序中搜索數據。我有3個JPA實體類,它們都擴展了BaseEntity類,每個類都由Lucene索引。例如:從任何索引實體進行Hibernate搜索搜索

@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標題或圖書的作者並得到結果列表,列表中。這是我到目前爲止:

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?謝謝!

回答

1

你要使用可變參數式方法的類,像這樣:

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, DVD.class, Book.class); 

這是因爲當Hibernate Search的創建搜索查詢時,它增加了類名(S)到查詢(對於_hibernate_class字段,這是索引類的名稱)。