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

标签 java hibernate-search

我的代码中的类 Credenciada 继承自 Cadastro 类,该类具有 CEP 类型的属性(嵌套类)“cep”。 CEP 类具有 String 类型的属性“uf”。

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

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

Cadastro.java:

@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
}

信用证.java:

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

    //many attrs and getters/setters here

}

CEP.java

@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();

异常(exception):

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)

最佳答案

Hibernate Search 5.6.1.Final and 5.7.0.Final have just been released ;这两个版本都包含针对您的问题的修复 ( HSEARCH-2587 )。

关于java - @SortableField 在嵌套实体中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42251819/

相关文章:

java - 使用分隔符在流中交错元素

java - 在 Java 中从 X.509 openssl 证书中提取 sha1

java - 没有科学记数法可以格式化 Double 吗?

java - 无法为类型 'x.y.z.package' 构建查询,该类型既未配置,也没有任何配置的子类型

lucene - Hibernate Search 自动更新组合字段

java - NetworkInfo 始终返回 null

java - jsp中不打印参数

java - 单击按钮时随机化数组中的颜色

amazon-web-services - 使用 ElasticSearch 进行索引存储的 AWS Elastisearch CodeBuild 集成测试与 Hibernate 搜索的访问策略

java - 使用 Hibernate Search + Infinispan Directory 的事务 ACID 属性