我的代码中的类 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/