1

ここで、Hibernate Search と Lucene で奇妙な問題に直面しています。この問題はちょっと単純だと思います。複数のレコードを返すものを検索している場合、すべて正常に機能します。1 つのレコードのみを返すものを検索している場合、エンティティ要素はすべて 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 + "]";
    }
}

そして、Hibernate Search で検索する私の DAO...

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)エラーも例外もなく、魅力的に機能します。しかし、クエリの結果が 1 つのレコードのみの場合、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());to System.out.println(c.toString());then は、すべての要素を印刷するのに最適です。

私は使っている:

  • Hibernate Search 4.3.0.Final;
  • Hibernate Core 4.2.2.Final;
  • 春 3.2.3.RELEASE;
  • ルセン 3.6.2;

私の質問を更新する:

Hibernate Search のバージョンを 4.4.0.Alpha1 にアップグレードしましたが、エラーが引き続き発生します。また、私のエンティティが「遅延ロード」に関連していることにも気付きました。エンティティを調べると、すべてのプロパティが null ですが、 という別のプロパティがありますJavassistLazyInitializer

4

2 に答える 2