• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problems using Hibernate Criteria API

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a class Person with these attributes: firstname:String, lastname:String and birthday:Date.

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?
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 162
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic