-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: HIBERNATE SEARCH (RESOLVED) How Validate field (search/sort)
PostPosted: Thu Sep 03, 2015 12:20 pm 
Newbie

Joined: Thu Sep 03, 2015 11:50 am
Posts: 3
Hello guys :)

I am using hibernate-search version 5.3.0.Final. I use it for search value and sort result.
I have defined my entities like this:
Code:
@Entity
@Indexed
public class Film {
...
    @Basic
    @Fields( {
        @Field,
        @Field(name = "title_sort", analyze = Analyze.NO)
    } )
    @Column(name = "title")
    public String getTitle() {
        return title;
    }
....
    @ManyToOne
    @IndexedEmbedded
    @JoinTable(.......)
    public Category getCategory() {
        return category;
    }
....
}

It's a basic use: entity with special sort field (title_sort) and manytoone attribute (category)

I want to detect invalid field when querying (with sort and search field):
Code:
       Query jpaQuery = queryBuilder
                                .keyword()
                                .wildcard()
                                .onField("title")
                                .matching(word)
                                .createQuery());
        .......
        org.hibernate.search.jpa.FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(globalQuery, className);
        .......
        SortField sortField = new SortField("title_sort", type, reverseSort);
        Sort sort = new Sort(sortField);
        jpaQuery.setSort(sort);
        .......
        // execute query and return results
        List entityList = jpaQuery.getResultList();

So in this case check search field "title" and also check sort field "title_sort"
I have two solutions:
A/ don't check field and catch exception NoSuchFieldException
B/ check field before querying

For solution A it works well for search field but for the sort the exception isn't fired
For solution B, it works but i was unable to check collection field (like categroy in the example):

Code:
HashMap<String,FieldDescriptor.Type> attributesMap =  new HashMap<>();

// doesn't work for collection field like category.name (if categroy class has field name)
FieldDescriptor fieldDescriptor = searchFactory.getIndexedTypeDescriptor(className).getIndexedField(field);

if (fieldDescriptor != null){
   attributesMap.put(cls.getName().toLowerCase() + '.' + field, fieldDescriptor.getType());
}
else
   logger.info("empty for: "+field);
.....
if (attributesTypeMap.containsKey(sort or search field)){
   // make query
  ....
}


What is the best solution for my problem ?
How handle it in case of A and/or B ?

Regards


Last edited by Javaman44 on Tue Sep 08, 2015 3:14 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: HIBERNATE SEARCH: How Validate field (search/sort) in query
PostPosted: Fri Sep 04, 2015 6:52 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
I think it depends on what you would like your application to do, when a wrong sorting field is chosen.
I would generally expect you to want to raise an exception to the higher level, so I'd catch the NoSuchFieldException and wrap it in a custom runtime exception with an appropriate error message?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: HIBERNATE SEARCH: How Validate field (search/sort) in query
PostPosted: Fri Sep 04, 2015 10:47 am 
Newbie

Joined: Thu Sep 03, 2015 11:50 am
Posts: 3
Hello sanne.grinovero !

Thanks for your answer. As i explain "nosuchfieldexception" is not fire in case of wrong Sort field but is fired for a wrong Search field.
Is there something wrong in my code ?

Regards


Top
 Profile  
 
 Post subject: RESOLVED: HIBERNATE SEARCH: How Validate field (search/sort)
PostPosted: Tue Sep 08, 2015 3:13 am 
Newbie

Joined: Thu Sep 03, 2015 11:50 am
Posts: 3
Behavior will be done in version 5.5
Please see issue on JIRA -> https://hibernate.atlassian.net/browse/HSEARCH-1986


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.