有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java@SortableField在嵌套实体中不起作用

我的代码具有类Credenciada,该类继承自类Cadastro,该类具有cep类型的atributte(嵌套类)“cep”。 CEP类具有字符串类型的atributte“uf”

uf atributte是用@SortableField注释的,因此索引是用“cep.uf”名称保存的。但是当我需要按“cep.uf”进行排序时,会抛出一个NullPointerException,因为Hibernate搜索没有找到这个索引/atributte

OBS:我看到类IndexedTypeDescriptorImpl的属性“allFieldDescriptors”没有“cep.uf”出现。因为在该方法的第139行中,在这一点上返回null“returnallfielddescriptors.get(fieldName)”

卡达斯特罗。爪哇:

@Entity
@Table(name = "CAD")
@Inheritance(strategy = InheritanceType.JOINED)
@Indexed
public class Cadastro extends Entidade<Long> {
    @Id
    private Long codigo;

    @IndexedEmbedded
    @NotAudited
    @ManyToOne
    private CEP cep;

    //more attrs and getters/setters here
}

克雷登西亚达。爪哇:

@Entity
@Indexed
@Table(name = "CAD_CRDC")
@PrimaryKeyJoinColumn(name = "CD_CAD_CRDC")
public class Credenciada extends Cadastro {

    //many attrs and getters/setters here

}

CEP。爪哇

@Entity
@Table(name = "CAD_CEP", schema = "dne")
public class CEP extends Entidade<Long> {

   @Id
   @Field(name = "cep", store = Store.YES)
   private Long nrCep;

   @SortableField
   @Field(store = Store.YES)
   private String uf;

}

搜索代码:

FullTextEntityManager fullTextEntityManager = hibernateUtil.getFullTextEntityManager();

QueryBuilder qb = HibernateSearchUtil.createQueryBuilder(fullTextEntityManager, Credenciada.class);     

BooleanJunction<?> criterios = qb.bool();

//many conditions here...

org.apache.lucene.search.Query rootQuery = criterios.isEmpty() ? new MatchAllDocsQuery() : criterios.createQuery();

FullTextQuery query = fullTextEntityManager.createFullTextQuery(rootQuery, Credenciada.class);

query.setSort(qb.sort().byField("cep.uf").createSort());

List resultList = query.getResultList();

例外情况:

java.lang.NullPointerException
    at org.hibernate.search.query.dsl.sort.impl.SortFieldStates.getCurrentSortFieldTypeFromMetamodel(SortFieldStates.java:156)
    at org.hibernate.search.query.dsl.sort.impl.SortFieldStates.determineCurrentSortFieldTypeAutomaticaly(SortFieldStates.java:149)
    at org.hibernate.search.query.dsl.sort.impl.ConnectedSortContext.byField(ConnectedSortContext.java:42)
    at br.gov.sindec.modulos.credenciada.repositorio.impl.CredenciadaRepositorioImpl.criarQueryListarCredenciadasIndexadas(CredenciadaRepositorioImpl.java:124)
    at br.gov.sindec.modulos.credenciada.repositorio.impl.CredenciadaRepositorioImpl.listarCredenciadasIndexadas(CredenciadaRepositorioImpl.java:52)

共 (1) 个答案