java-如何在相关实体中搜索(休眠搜索)
作者:互联网
我无处可试.我的要求是按记录名称搜索记录
以下是我的相关课程:
RecordFolderAnalysis.java
@Indexed
public class RecordFolderAnalysis extends AuditableEntity implements Serializable {
@ManyToOne
@JoinColumn(name = "arrivalId", nullable = false)
@ContainedIn
private RecordFolderArrival recordFolderArrival;
}
RecordFolderArrival.java
@Indexed
public class RecordFolderArrival extends BaseEntity implements Serializable
{
@Column(name="recordName", unique = true, nullable = false)
@Field(index = Index.UN_TOKENIZED)
private String recordName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "recordFolderArrival", fetch = FetchType.LAZY, targetEntity = RecordFolderAnalysis.class)
@IndexedEmbedded
private List<RecordFolderAnalysis> recordFolderAnalysis=new ArrayList<>();
}
以下是我的DAO类方法:
@Override
public List<T> search(final String queryString, final String... fields) {
List searchResult = hibernateTemplate.executeFind(new HibernateCallback<Object>() {
org.hibernate.Query fullTextQuery1 = null;
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
try {
System.out.println("in do in hibernate");
FullTextSession fullTextSession = Search.getFullTextSession(session);
Transaction tx = fullTextSession.beginTransaction();
QueryBuilder builder=fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(persistentClass).get();
Query luceneQuery=builder.keyword().onField("recordFolderArrival.recordName").matching(queryString).createQuery();
fullTextQuery1 = fullTextSession.createFullTextQuery(luceneQuery);
for (Object o : fullTextQuery1.list()) {
System.out.println("Values" + o);
}
} catch (Exception e) {
e.printStackTrace();
}
return fullTextQuery1.list();
}
});
return searchResult;
}
所以我通过将字段名称设置为recordFolderArrival.recordName来按Record-Name搜索.
但是它抛出了一个例外的说法
recordFolderArrival.recordName field does’nt exist in
RecordFolderAnalysis.
我是Hibernate Search的新手,所以如果有人可以帮助我解决这个问题.
谢谢,不要担心@Entity和其他注释会放在必须放置的地方,只是我没有在代码段中包含它们.
解决方法:
好的,我自己找到了答案,将注解面@IndexEmbedded更改为其他类和@ContainedIn,所以现在我的字段是
@IndexedEmbedded
private RecordFolderArrival recordFolderArrival;
和
@ContainedIn
private List<RecordFolderAnalysis> recordFolderAnalysis=new ArrayList<>();
解决了问题,因为@IndexedEmbedded是您可以导航到的属性
您的查询;我发布了答案,因为它可能对其他人有所帮助.
标签:hibernate-search,search,hibernate,lucene,java 来源: https://codeday.me/bug/20191031/1978379.html