gpt4 book ai didi

java - 如何在相关实体中搜索(Hibernate 搜索)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:39 24 4
gpt4 key购买 nike

我没有尝试这个东西。我的要求是按他们的名字搜索记录

以下是我的相关类(class):

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 是您可以导航到的属性您的查询;我发布了答案,因为它可能对其他人有帮助。

关于java - 如何在相关实体中搜索(Hibernate 搜索),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12852557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com