Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help on HQL

 
Marco Moreno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm peforming the following HQL but get 0 results, but according to database I should get 1 result. Now if I execute the SQL generated by this HQL query I get a result, which is correct.

Any ideas why HQL is not working properly?
==========================================================
Participant p = (Participant)getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria c = session.createCriteria(Participant.class);
c.add(Restrictions.eq(propertyName, value));
List l = c.list();
if (l == null || l.size() == 0) {
return null;
}
if (l.size() > 1) { // this should never happen...
throw new HibernateException("More than one Participant found with " + propertyName + " = " + value);
}
return (Participant)l.get(0);
}
}
);

=====================================================

Thanks!!!
[ September 10, 2007: Message edited by: Bear Bibeault ]
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, it is a Hibernate Criteria query, rather than HQL.
Second, I think it will help to start with a simpler version of this code (without callbacks, with constant property name/value) and see at which level of complexity it stops working as expected.
 
Marco Moreno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not get any results when I take HibernateCallback out. But I get results when I take out restrictions and have something like this:
Criteria c = getSession().createCriteria(Participant.class);
//c.add(Restrictions.eq(propertyName, value));
List l = c.list();

But when I add the restriction I get no results. Now I took the SQL generated by this, execute it and it did returned result. So I guess the Restrictions is not working properly?
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To have a chance of understanding what is going on here, one needs to get
specific: what property and value you specify, what SQL you get generated
and execute, how the Participant class is defined and mapped, what is in the database,... I think one can start with this list
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic