如何在相关实体中搜索(休眠搜索) [英] How to search in related entity (Hibernate search)

查看:75
本文介绍了如何在相关实体中搜索(休眠搜索)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有办法尝试这种方法.我的要求是按记录名称搜索记录

I am getting no where trying this thing .My requirement is to search records by their name

以下是我的相关课程:

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搜索. 但是它抛出了一个异常的说法

So I am searching by Record-Name by setting field name as recordFolderArrival.recordName. But It is throwing an exception saying

recordFolderArrival.recordName字段不存在 RecordFolderAnalysis.

recordFolderArrival.recordName field does'nt exist in RecordFolderAnalysis.

我对Hibernate Search还是陌生的,所以如果有人可以帮助我解决这个问题. 谢谢,不要担心@Entity和其他注释会放在必须放置的地方,只是我没有将它们包含在代码段中.

I am very new to Hibernate Search so if anyone can help me working this thing out. Thanks and Don't worry about @Entity and other annotation they are placed where they have to be its just I have not included them in snippet.

推荐答案

好吧,我自己找到了答案,我将注释面@IndexEmbedded更改为其他类,同时又将@ContainedIn更改为现在的字段

Ok I found the answer myself I changed the annotation sides @IndexEmbedded to other class and @ContainedIn as well so now my fields are

@IndexedEmbedded
    private RecordFolderArrival recordFolderArrival;

还有

  @ContainedIn
    private List<RecordFolderAnalysis> recordFolderAnalysis=new ArrayList<>();

问题已解决,因为@IndexedEmbedded是您可以导航到的属性 您的查询;我发布了答案,因为它可能对其他人有帮助.

Problem solved because @IndexedEmbedded is the property you can navigate to in your queries; I posted the answer cause it might be helpful for others.

这篇关于如何在相关实体中搜索(休眠搜索)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆