Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Hibernate Criteria question

  Asked By: James    Date: Mar 22    Category: Java    Views: 1808
  

I have to build a search page for my application. I have a one-to-many relation between two entities.
I want to create a search criteria to find something in "-many" side. For example, I have a document
that has a list of tags. I want to find documents that have a given tag.
what can I do?
I am using hibernate 3 with annotations and criteria API.

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Ludo Ricci     Answered On: Mar 22

Imagine that A has a collection of Bs. Here is the technique. Join all the objects in the criteria  and then start adding the where clause but here is your query in its general form :



Query query=getSession().createQuery("from A a left outer join a.B b where b.x< :something").

setParameter("something", 10 /*for example*/);

return query.list();



Here is what I always do. I google "hibernate reference + hql". The first result will take you to a hibernate  Heaven. You can find  more complex examples out there....

 
Answer #2    Answered By: Luigi Fischer     Answered On: Mar 22

I do not worked with Criteria, but you can use below query:
FROM Document AS Document WHERE ? IN elements(Document.Tags)

 
Answer #3    Answered By: Latasha Wilson     Answered On: Mar 22

I have a example  of your problem wich work correctly



you can try it .



Criteria criteria  = session.createCriteria(Document.class)
.createAlias("pages", "pageAlias")

.add(Restrictions.eq(pageAlias.id", pages.getId())



you should first set a Alias name for a many side  of relation   if you imagine that there is a Document that has a Set of page  , then for page Object you set a pageAlias .

then add Restrictions like above . it finds your Object from a set or list  of objects by using

add(Restrictions.eq(pageAlias.id", pages.getId()) .



the code is like :



public class Document {



private Set Pages

........

}

 
Answer #4    Answered By: Ora Hanson     Answered On: Mar 22

Thank you for your answers. I have more than 30 criterion to search  and this is only one of them!
I have to use criteria  API. So if you now the right criterion to use, let me know.

I have used this code:

DetachedCriteria criteria = DetachedCriteria.forClass(Document.class);
Criterion tagCriterion = Restrictions.in("tags",new String[] {"TAG1"});
criteria.add( tagCriterion );

this code throws an exception:

could not execute query;
nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
...
No value specified for parameter 1

Any help?

 
Answer #5    Answered By: Angel Watkins     Answered On: Mar 22

"No value specified for parameter 1" means the first parameter that you have considered in your query, does not have any value assigned to it.

Look at the following sample :

Query query=getSession().createQuery("from A a left outer join a.B b where b.x< :something");

return query.list();



look at the parameter "something" in the query. The code above will throw the same exception that you received because this line is missing before query.list():

query.setParameter("something", 10);



If I were you, I would use HQL for such a long where clause along with a WildCardHelper for Strings. I mean there are times that you need to add such clauses to your criteria  :

where b.x like "%" + something or

where b.x like something + "%" or

where b.x like "%" + something + "%"



I always let users to use a wildcard like * on the search  fields. and then I analyse the user entry and turn it to the above examples using a helper class(e.g. em* will result in "emil", "emily", ....) . There are also cases where you want to search something among a string where you can automatically load it with MatchMode Anywhere( "%" + something + "%"). Anyway In my opinion HQL is much easier when you have a search page  with multiple items to search. First left outer join the entities that you are to search your information upon and then add the user input on the where clause....

 
Answer #6    Answered By: Burkett Bernard     Answered On: Mar 22

Thank you for your solution. It works fine. I no need to any join or anything else!

 
Answer #7    Answered By: Perdita Lopez     Answered On: Mar 22

Hibernate criteria  API could handle this situation without the need to write HQL. Consider that A has a collection of B and you what to put criteria on b attributes then here is what we can do with criteria API:



Criteria criteria = getSession().createCriteria(A.class).setFetchMode("b", FetchMode.JOIN);

criteria.add(Restrictions.eq("b.attribute1",somthing);



I‘ve used it many time and it worked. You can deal with more complicated queries with this technique but be careful of performance in some situations.

 
Didn't find what you were looking for? Find more on Hibernate Criteria question Or get search suggestion and latest updates.




Tagged: