permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes hibernate: two 'like' clauses in sql fails. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate: two Watch "hibernate: two New topic
Author

hibernate: two 'like' clauses in sql fails.

davidkiwi
Greenhorn

Joined: Sep 05, 2002
Posts: 3
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

Joined: Sep 05, 2002
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate: two 'like' clauses in sql fails.