Criteria query restrictions are not applied in the items of the collections. I have a persistence entity (POJO) Menugroup and it has a collection called catgroups (HashSet, one to many). Catgroup persistence entity (POJO) has another one-to-many collection "dealitems" (HashSet). My criteria query is below:
Mapping file snippet of Menugroup.hbm.xml is below:-
<set inverse="true" batch-size="10" lazy="false" name="catgroups">
<comment>Menu Group relationship</comment>
When I run my Criteria query, hibernate generates the below 2 sqls and they are displayed in the console
Hibernate generated SQL query-1
Hibernate generated SQL query-2:
The issue issue is, in the result, items in the catgroups collections are not in ascending order and the restriction "published=true" are also not applied on them. I meant to say that below condtions from my criteria query are not applied on catgroups collections.
add( Restrictions.eq("published", true) )
The above critieria conditions should be applied on hibernate generated 2nd SQL query but it does not happen. Could any one help me to find out whether I have missed some thing in my criteria query to get the result as I expected or this is a bug in criteria query. ?
Expected result: Items in the catgroups collection should be in order and the condition "published"=true should applied.
I hope, the information i provided above would help to understand what is the problem I face.
Also, this is the type of query where I like using HQL instead of Criteria. I find that the more complex queries are easier for me to do in HQL then using the Criteria object.
This is just my opinion, and should not be thought of as the way you have to do it.
Originally posted by Saravanan Vijayappan:
I would like to have a single object. My intention is to have everything in single object and navigating later whenever it is required. Like, a List should have Menugroup objects, navigating to Catgroups through from Menugroup object through Iteration later. I have found some difficulties in HQL that join HQL query does not return POJO objects and it returns java.lang.Object types. Please help me if you can give me corresponding HQL query for my criteria query. Thanks
Yeah, an HQL will return that if you are specifying more in your select portion, but if you have the classes mapped and you don't specify the Select part, then it will create POJOs.
What HQL did you try?
My HQL is below
Output is below: all objects in the list are java.lang.Object and they are not type of Menugroup POJO.
One more issue what I'm seeing is, published=true condition is not applied in the 2nd SQLs while batch is running to fetch the child collection (Catgroups)
I have been working since long to resolve this, but, I am not able to do, Please help me :-((
Many Thanks in advance!
[ July 14, 2008: Message edited by: Saravanan Vijayappan ]
so you have
Query query = session.createQuery("from Menugroup m left join m.catgroups c where m.published=true and c.published=true");
List list = query.list();
You can try changing
List list = query.list();
List<Menugroup> list = query.list();
or when you loop through the objects in the list, just cast them to Menugroup objects. They are Menugroup objects in the Collection.
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.model.persistence.Menugroup
Any suggestion please!!!?