This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes Hibernate Criteria Query not working properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Criteria Query not working properly" Watch "Hibernate Criteria Query not working properly" New topic
Author

Hibernate Criteria Query not working properly

Saravanan Vijayappan
Ranch Hand

Joined: Jan 02, 2007
Posts: 47
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


Cheers,<br />Sarav
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Saravanan Vijayappan
Ranch Hand

Joined: Jan 02, 2007
Posts: 47
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
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jan 02, 2007
Posts: 47
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
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jan 02, 2007
Posts: 47
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

Joined: Jan 02, 2007
Posts: 47
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate Criteria Query not working properly