| Author |
Problems using Hibernate Criteria API
|
Kristofer Hindersson
Greenhorn
Joined: Jan 19, 2010
Posts: 25
|
|
Hi, I have a class Person with these attributes: firstname:String, lastname:String and birthday ate.
I'm trying to use the Hibernate Criteria API by creating a new Person and setting some of the attributes with values that I want to query the database against.
Person person = new Person();
person.setFirstname("Paul");
I then create a new Example thus:
Example example = Example.create(person);
example.enableLike();
example.excludeZeroes();
...after which I create the Criteria:
...createCriteria(Person.class).add(example).list();
The problem is that this doesn't return any result (empty list) even though I know for certain that there is row where the firstname-column contains 'Paul'. What am I doing wrong here? Have I completely misunderstood the usage of Example together with Criteria?
|
 |
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Joined: Aug 26, 2006
Posts: 4932
|
|
No, it looks like you're doing it correctly.
Why not try a query like the following, without the Example. The following should just return all the data from the database. See if you can do a criteria query without the Example first. Then, when that works, let's move onto seeing if the Example class is the problem.
Whadya think?
|
Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
|
 |
Kristofer Hindersson
Greenhorn
Joined: Jan 19, 2010
Posts: 25
|
|
Cameron Wallace McKenzie wrote:No, it looks like you're doing it correctly.
Why not try a query like the following, without the Example. The following should just return all the data from the database. See if you can do a criteria query without the Example first. Then, when that works, let's move onto seeing if the Example class is the problem.
Whadya think?
Well if I use FetchMode START or ANYWHERE, or if I use the default (EXACT) and sets more than one attribute in the Person-instance, qbe will work. But if I only set the firstname-attribute before creating the example, the Criteria won't return any result. I wonder if this has anything to do with that I have more than one 'Paul' present in the database?
|
 |
Kuladip Yadav
Ranch Hand
Joined: Jul 30, 2008
Posts: 162
|
|
Can you check query generated by hibernate for both scenario ?
By looking at both queries you will come to know exact problem.
|
 |
Kristofer Hindersson
Greenhorn
Joined: Jan 19, 2010
Posts: 25
|
|
Thanks for the replies. I got it working by rebuilding the whole project (Spring MVC). I have no idea what I did differently but now I have another problem:
I have the following classes:
Here's my Criteria method in my ServiceImpl:
pretty straighforward was my thought, but this Criteria causes the following exception:
|
 |
 |
|
|
subject: Problems using Hibernate Criteria API
|
|
|