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

Problems using Hibernate Criteria API

 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a class Person with these attributes: firstname:String, lastname:String and birthdayate.

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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kristofer Hindersson
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic