Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Problem in Query By Example (Hibernate)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I am using hibernate3 in my application. When executing the below code
Product exampleProduct=new Product();
exampleProduct.setPrice(22);
Example example=Example.create(exampleProduct);
Criteria criteria=session.createCriteria(Product.class);
criteria.add( example );
List results = criteria.list();

it works fine. Lists all the objects with the price is 22. when modified the code as below

Product exampleProduct=new Product();
exampleProduct.setName("Product 2");

Example example=Example.create(exampleProduct);
Criteria criteria=session.createCriteria(Product.class);
criteria.add( example );
List results = criteria.list();

returns the empty list but the database has few products with the name "Product 2". When I checked the query generated,
Hibernate: select this_.id as id0_0_, this_.name1 as name2_0_0_, this_.description as descript3_0_0_, this_.price as price0_0_, this_.supplierId as supplierId0_0_ from product2 this_ where (this_.name1=? and this_.price=?)

the where condition sets parameter value for price. I have executed this code with different properties of the product object but the generated query always adds price parameter value set and returns empty list.

Is it a bug and is it overcomed in the latest version of hibernate or do I need to set any property for proper working of this code..
 
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
There are settings for not ignoring null or not ignoring zeros. Maybe it's seeing a zero and doing a query based on it?





Using the Criteria API from Hibernate: Tutorial

-Cameron McKenzie
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your hbm and the Hibernate POJO for Product.

If you have defined the "price" field in "Product" as "int", you should change it so that it is an "Integer" and then test the criteria query.
 
Lakshmi Natarajan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cameron and Rahul. Both the solutions have worked out for the query.
 
reply
    Bookmark Topic Watch Topic
  • New Topic