我在 Hibernate Search 和 Lucene 中遇到了一个奇怪的问题。我相信这个问题有点简单:当我搜索返回多条记录的东西时,一切正常。当我搜索只返回一条记录的东西时,实体元素都是空的,但是当我调用该toString()
方法时,一切都在那里。
我的实体:
import java.io.Serializable;
import java.sql.Blob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.search.annotations.Analyze;
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
@Table(name = "vw_colaborador", schema = "col")
public class Colaborador implements Serializable {
private static final long serialVersionUID = 886370076152195975L;
@Id
@Column(name = "id", insertable = false, updatable = false)
private Long id;
@Column(name = "matricula", insertable = false, updatable = false)
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
private String matricula;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "nome", insertable = false, updatable = false)
private String nome;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "email", insertable = false, updatable = false)
private String email;
@Column(name = "foto", insertable = false, updatable = false)
private Blob foto;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "login", insertable = false, updatable = false)
private String login;
@Field(store = Store.YES, index = Index.YES, analyze = Analyze.YES)
@Column(name = "ramal", insertable = false, updatable = false)
private String ramal;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "hierarquia_ua", insertable = false, updatable = false)
private String ua;
@Column(name = "situacao_id", insertable = false, updatable = false)
private Long situacaoId;
@Column(name = "situacao", insertable = false, updatable = false)
private String situacao;
@Column(name = "vinculo_id", insertable = false, updatable = false)
private Long vinculoId;
@Column(name = "vinculo", insertable = false, updatable = false)
private String vinculo;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "diretoria", insertable = false, updatable = false)
private String diretoria;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "sigla_diretoria", insertable = false, updatable = false)
private String siglaDiretoria;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "sala", insertable = false, updatable = false)
private String sala;
@Field(store = Store.NO, index = Index.YES, analyze = Analyze.YES)
@Column(name = "predio", insertable = false, updatable = false)
private String predio;
//... getters and setters ommited
@Override
public String toString() {
return "Colaborador [id=" + id + ", matricula=" + matricula + ", nome=" + nome + ", email=" + email + ", foto=" + foto + ", login=" + login + ", ramal=" + ramal + ", ua="
+ ua + ", situacaoId=" + situacaoId + ", situacao=" + situacao + ", vinculoId=" + vinculoId + ", vinculo=" + vinculo + ", diretoria=" + diretoria
+ ", siglaDiretoria=" + siglaDiretoria + ", sala=" + sala + ", predio=" + predio + "]";
}
}
还有我的 DAO,我在其中使用 Hibernate Search 进行搜索...
ColaboradorDAO(由 Spring 管理)
import java.util.List;
import javax.persistence.TypedQuery;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.FullTextQuery;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.stereotype.Repository;
@Repository("colaboradorDAO")
public class ColaboradorDAO extends AbstractDAO<Colaborador, Long> {
private static final Logger logger = LogManager.getLogger(ColaboradorDAO.class);
@SuppressWarnings("unchecked")
public List<Colaborador> filtrar(Object termo) throws DAOException {
try {
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(this.entityManager);
QueryBuilder qb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Colaborador.class).get();
Query query = qb.keyword()
.onFields("matricula", "nome", "email", "login", "ramal", "ua", "diretoria", "siglaDiretoria", "sala", "predio")
.matching(termo)
.createQuery();
FullTextQuery fullTextQuery = fullTextEm.createFullTextQuery(query);
Sort sortField = new Sort(new SortField("nome", SortField.STRING));
fullTextQuery.setSort(sortField);
return fullTextQuery.getResultList();
}
catch (Exception e) {
logger.error("Erro ao filtrar colaboradores com termo: " + termo, e);
throw new DAOException(e);
}
}
public void indexar() throws DAOException {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(this.entityManager);
try {
fullTextEntityManager.createIndexer().startAndWait();
}
catch (InterruptedException e) {
logger.error("Erro ao criar indice de busca dos colaboradores", e);
throw new DAOException(e);
}
}
}
当我调用该filtrar(String termo)
方法时,它就像一个没有错误和异常的魅力。但是当我查询的结果只有一条记录时,Colaborador
实体被实例化但所有元素都是null
.
例子:
@Test
public void testColaborador() {
try {
List<Colaborador> listColaborador = this.colaboradorService.filtrar("060091");
for (Colaborador c : listColaborador) {
System.out.println(c.getNome());
}
}
catch (ServiceException e) {
e.printStackTrace();
}
}
结果:
null
如果我更改行:System.out.println(c.getNome());
到System.out.println(c.toString());
then,则非常适合打印出所有元素。
我在用:
- Hibernate Search 4.3.0.Final;
- Hibernate Core 4.2.2.Final;
- Spring 3.2.3.RELEASE;
- Lucene 3.6.2;
更新我的问题:
我已将 Hibernate Search 版本升级到 4.4.0.Alpha1 并且错误仍然存在。我还意识到我的实体与“延迟加载”有关。当我检查我的实体时,所有属性都为空,但还有另一个属性名为JavassistLazyInitializer
.