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.  [ 5 posts ] 
Author Message
 Post subject: Problem with Criteria.list()
PostPosted: Sat Jun 14, 2008 7:25 am 
Newbie

Joined: Thu Mar 13, 2008 6:18 am
Posts: 4
Hi,

I am trying to fetch all entities from the database, filtered by a given entity A which is in a many-to-one relationship with another entity B.

The problem is in the select statement generated by Hibernate. In the WHERE clause, only the fields from entity A appear, not the ones from
the joined entity B. So if I change the fields in B, but not the fields in A, List<T> ret from the code below will contain one element, even
though there is no such entity in the database.


Code:
            List<T> ret = null;

            Session session = getSession();
            Criteria crit=session.createCriteria(getPersistentClass());
            Example ex= Example.create(entity);     // entity A
           
            crit.add(ex);
            ret=crit.list();




What am I doing wrong ?

Any help would be highly appreciated.
Thanks.

Andrei


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 8:36 am 
Regular
Regular

Joined: Sun Apr 13, 2008 3:04 am
Posts: 71
Location: Bangalore
Try the query some thing like this

Session session = HibernateUtil.getSession();
Criteria cr = session.createCriteria(Relation.class);
cr.add(Restrictions.ilike(Relation.FIELD_RELATION, aRelation));
cr.createCriteria(UserAssociated.FIELD_USER).add(
Restrictions.ilike(User.FIELD_LOGIN_ID, aLoginID));
Relation lRelation = (Relation) cr.uniqueResult();

instead of using example etc..

Regards,
Nagendra

_________________
Raja Nagendra Kumar,
C.T.O
http://www.tejasoft.com
TejaSoft - Specialists in Code Audit, Unit Testing and Merciless Re-factoring - Engineering Crisis Turnaround Experts


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:53 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, check out the Restrictions class. Here's the Hibernate JavaDoc for the Criterion Restrictions class:

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/Restrictions.html


Quote:
static Criterion
ilike(String propertyName, Object value)
A case-insensitive "like", similar to Postgres ilike operator
static Criterion

ilike(String propertyName, String value, MatchMode matchMode)
A case-insensitive "like", similar to Postgres ilike operator


For more examples, you can check out this tutorial of mine:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=09howtousethecriteriaapi

This tutorial does a good job of contrasting what you can do with the Example class and what you can do with the Restrictions class of the hibernate Criteria API.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Last edited by Cameron McKenzie on Tue Jun 17, 2008 3:51 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 9:04 pm 
Newbie

Joined: Thu Mar 13, 2008 6:18 am
Posts: 4
Thanks a lot for the help and the tutorial. Now it works perfect.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 3:53 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Happy to be of assistance. :)

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.