Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate: two 'like' clauses in sql fails.

 
davidkiwi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My database is mysql4.1.1, when I query it via hibernate with just one 'like' expression, it works fine, eg:

Criteria criteria = session.createCriteria(npd.hibernate.Plant.class);
criteria.add( Expression.like("genus", plantForm.getGenus() +"%" ));
list = criteria.list();

The sql that's generated is:
Hibernate: select this.id as id0_, this.active as active0_, this.genus as genus0_, this.species as species0_ from plant this where this.genus like ? order by this.genus asc, this.species asc
(where the parameter is 'a%')


But when I add another 'like' expression, the criteria is ignored altogether and I get the entire database back, eg:

Criteria criteria = session.createCriteria(npd.hibernate.Plant.class);
criteria.add( Expression.like("genus", plantForm.getGenus() +"%" ));
criteria.add( Expression.like("species", plantForm.getSpecies() + "%"));
list = criteria.list();

The sql that's generated is:
Hibernate: select this.id as id0_, this.active as active0_, this.genus as genus0_, this.species as species0_ from plant this where this.genus like ? and this.species like ? order by this.genus asc, this.species asc
(where the parameters are 'a%' and 'a%')

The generated sql works just fine against the database.

Thank you, any assistance would be greatly appreciated.
 
davidkiwi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case anyone is interested, I replaced the MySql database with a Hsqldb database, the queries (as well as inserts, updates and deletes) now work perfectly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic