-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Query by Example binding more than assigned parameters
PostPosted: Fri Jul 21, 2006 9:36 am 
Newbie

Joined: Thu Aug 11, 2005 12:11 pm
Posts: 8
Hibernate version: 3.1

Name and version of the database you are using: MySql 4.0.18

The generated SQL (show_sql=true):
Hibernate: select this_.id as id34_0_, this_.name as name34_0_, this_.product_id as product3_34_0_, this_.initialowner as initialo4_34_0_, this_.initialqacontact as initialq5_34_0_, this_.description as descript6_34_0_ from bugs.components this_ where (this_.product_id=? and this_.initialowner=?) order by this_.name asc

Debug level Hibernate log excerpt:
DEBUG ShortType:74 - binding '1' to parameter: 1
DEBUG IntegerType:74 - binding '0' to parameter: 2

Ok, here's my problem. I've looked at the Ref docs on Querying by example, and on the forums, but I havent been able to find a cause for what I'm experienceing. I'm creating an object and assigning a SINGLE parameter to it, and then using that instance as an example to query by. HOWEVER, as is show above in the Debug, its binding TWO parameters...neither of which are an ID, or in anyay connected to a primary key.

Why would hibernate be creating a default value an binding it to a column when I never specified a paramter? This is happening for all my query by Examples, and the other parameter it picks is seemingly random.

My code follows:
Code:
Business Object:
      Profiles profile = new Profiles();
      profile.setLoginName(email);

      new ProfilesHome().findByExample(profile);

      ...

Data Access Object:
public List findByExample(Profiles instance) {
      List results = sessionFactory.getCurrentSession().
                         createCriteria(Profiles.class).
                         add(Example.create(instance)).list();
     return results;
}


Top
 Profile  
 
 Post subject: Solved
PostPosted: Fri Jul 21, 2006 11:05 am 
Newbie

Joined: Thu Aug 11, 2005 12:11 pm
Posts: 8
Well, in thinking too much of it being Hibernate's fault, hibernate was in fact doing what it was supposed to do. Basic, fundamental java came back to bite me.

The Instances I was using to query on have basic java types--not all of them are wrapped in classes. Thus, when instantiating a new instance of the object with basic types, those basic types were given their default values, and hibernate, being the good piece of sofware it is, saw this value and bound it to the query by example.

To rectify this, I've added a quantifier to the example query:
Code:
List results = HibernateUtil.getSession().createCriteria(Profiles.class).
                   add(Example.create(instance).
                   excludeProperty("propertyWithBasicType")).
                   addOrder(Order.asc("name")).list();


...oops


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.