3

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

Following are my related classes:

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

  }

Follwing is my DAO class method:

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

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

recordFolderArrival.recordName field does'nt exist in RecordFolderAnalysis.

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.

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52

2 Answers2

5

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;

And

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

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.

Dangling Piyush
  • 3,658
  • 8
  • 37
  • 52
-2

I know about the advantages of QueryBuilders, e.g. type safety. But personally, I still find them horrible, as you have to write lots of stuff for simple queries which results in a code not easy to understand.

So, I would prefer to use a JPQL in your case, which looks pretty much like SQL, but handles objects and their attributes and relations. Make it a NamedQuery on your Entity and use parameters for the variable values (instead of String concatenation) and you should be able to solve your query much easier.

Alexander Rühl
  • 6,769
  • 9
  • 53
  • 96