gpt4 book ai didi

java - 当结果是一条记录时,Hibernate Search/Lucene 返回 null 元素

转载 作者:行者123 更新时间:2023-11-30 04:15:23 24 4
gpt4 key购买 nike

我在 Hibernate Search 和 Lucene 方面遇到了一个奇怪的问题。我相信这个问题有点简单:当我搜索返回多个记录的内容时,一切正常。当我搜索仅返回一条记录的内容时,实体元素全部为 null,但是当我调用 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());然后,非常适合打印所有元素。

我正在使用:

  • Hibernate 搜索 4.3.0.Final;
  • Hibernate 核心 4.2.2.Final;
  • Spring 3.2.3.RELEASE;
  • Lucene 3.6.2;

更新我的问题:

我已将 Hibernate Search 版本升级到 4.4.0.Alpha1,但错误仍然存​​在。我还意识到我的实体与“延迟加载”有关。当我检查实体时,所有属性均为 null,但还有另一个名为 JavassistLazyInitializer 的属性。

最佳答案

好了,问题解决了!我无法对实体上的方法 getter 和 setter 使用 final 关键字。当我注释 getter 而不是 private 字段时,就像 @Sanne 所说,此错误消息显示在控制台上:

ERROR PojoEntityTuplizer:201 - HHH000112: Getters of lazy classes cannot be final

然后,我删除了我的实体 Colaborador 的所有最终关键字,并且成功了!所以,我改回了私有(private)字段的注释,它仍然有效!

谢谢!

关于java - 当结果是一条记录时,Hibernate Search/Lucene 返回 null 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18539734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com