• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate Criteria Query not working properly

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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:


code:



Mapping file snippet of Menugroup.hbm.xml is below:-

code:

<set inverse="true" batch-size="10" lazy="false" name="catgroups">
<key>
<column name="MENUGROUP_ID">
<comment>Menu Group relationship</comment>
</column>
</key>
<one-to-many class="com.model.persistence.Catgroup"/>
</set>


When I run my Criteria query, hibernate generates the below 2 sqls and they are displayed in the console

Hibernate generated SQL query-1

code:



Hibernate generated SQL query-2:


code:




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.

createCriteria("catgroups").addOrder( Property.forName("name").asc()).
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.

Thanks
Sarav
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, not sure if I can help, but do you want them as two seperate Criteria objects, or just one?

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.

Mark
 
Saravanan Vijayappan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

Mark
 
Saravanan Vijayappan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Mark.

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!
Sarav
[ July 14, 2008: Message edited by: Saravanan Vijayappan ]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, yes, that is true based on how you wrote your code.

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

to

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.

Mark
Mark
 
Saravanan Vijayappan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, it does not work. I am getting ClassCastException when I try cast it while looping. So, the objects in the List is java.lang.Object.




Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.model.persistence.Menugroup

Any suggestion please!!!?

Many Thanks
Sarav
 
Saravanan Vijayappan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is simple/powerful HQL solved my requirement.

select distinct m from Menugroup m left join fetch m.catgroups c where m.published=true and c.published=true order by m.name
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic