-->
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.  [ 6 posts ] 
Author Message
 Post subject: Possible bug with 3.1?
PostPosted: Tue Dec 20, 2005 4:37 pm 
Newbie

Joined: Wed Sep 17, 2003 5:36 pm
Posts: 5
Hi,

I have a named query that used to work before 3.1. Here is the named query:
Code:
   <query name="Scan.byUnreviewedStatus.withDuplicateScales">
      <![CDATA[
       select scan
         from com.gemex.scan.Scan as scan
        where scan.productPrefix = :productPrefix
          and scan.id != :id
          and scan.whiteLightRawValue > (:whiteLightRawValue - .0001)
          and scan.whiteLightRawValue < (:whiteLightRawValue + .0001)
          and scan.colorLightRawValue > (:colorLightRawValue - .0001)
          and scan.colorLightRawValue < (:colorLightRawValue + .0001)
          and scan.scintillationRawValue > (:scintillationRawValue - .0001)
          and scan.scintillationRawValue < (:scintillationRawValue + .0001)
          and (scan.scanStatusCode = :unreviewedScanStatusCode or scan.scanStatusCode = :skippedScanStatusCode)         
      ]]> 
   </query>


After going to 3.1, I get an error that basically said it cannot set the value for parameter, whiteLightRawValue. When I checked the debug statement, it is trying to set the value as a DoubleType. However, the field, whiteLightRawValue, is actually a Float type.

Hibernate version: 3.1
Database and version: MySQL 5.0.16

Here is the code to set the parameter:
Code:
      Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);

      try {         
         Query query = session.getNamedQuery("Scan.byUnreviewedStatus.withDuplicateScales");
         query.setParameter("id", scan.getId());         
         query.setParameter("unreviewedScanStatusCode", unreviewedScanStatusCode);
         query.setParameter("skippedScanStatusCode", skippedScanStatusCode);
         query.setParameter("productPrefix", scan.getProductPrefix());
         query.setFloat("whiteLightRawValue", scan.getWhiteLightRawValue());
         query.setFloat("colorLightRawValue", scan.getColorLightRawValue());
         query.setFloat("scintillationRawValue", scan.getScintillationRawValue());       

         unreviewedScansWithDuplicateScales = query.list();
         
         return unreviewedScansWithDuplicateScales;
      }



Here is the snippet of the mapping between the database column and the Java field:
Code:
<property name="whiteLightRawValue" type="java.lang.Float" >
   <column name="WhiteLightRawValue" length="12" not-null="false" sql-type="float" />
</property>


If anyone need the entire mapping file or persistent class, I can provide them but they are rather long. Basically, the MySQL column for WhiteLightRawValue is defined as a float in MySQL and the mapped field in the persisent class is a Java Float.

One last thing, when I changed the code to explicitly set the parameter as a Float type (see below), then it works. But as I stated before, this worked before 3.1, so I am guessing that's a bug in 3.1?

Code:
query.setFloat("whiteLightRawValue", scan.getWhiteLightRawValue());
query.setFloat("colorLightRawValue", scan.getColorLightRawValue());
query.setFloat("scintillationRawValue", scan.getScintillationRawValue());
       


Thanks,
Ben


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 6:18 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
hibernate 3.1 have stronger type checking thane previous versions - you have to set
same type in property and parameter
I don't sure is it mistake - i like parameter conversion for simpler types (number, string etc),too


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 10:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Yes there was a change in the way unspecified parameter types are guessed in 3.1. In 3.1, the query parser attempts to guess an expected type for any parameters it encounters based on the context of the tree around the parameter. This is all new code, so it is likely I made it a little too restrictive.

The old code was solely based on the java type of the passed parameter value to bind, which in more cases than not resulted in bad choices.

Either post the syntax tree from the parser for that query, or open an enhancement request in JIRA with a runnable case using that query.

FWIW, the new code should work with your query if you change around the structure of the where clause expressions a bit:
Code:
    and ( scan.whiteLightRawValue + .0001 ) > :whiteLightRawValue
    and ( scan.whiteLightRawValue - .0001 ) < :whiteLightRawValue 

The "expected type guessing" here would be based on the left and right hand sides of the '<' and '>' comparisons. In the modified query, that should correctly resolve to float since scan.whiteLightRawValue is mapped as float; thus :whiteLightRawValue would be expected to be of type float. I would guess that the literal '.0001' is not being typed correctly by the parser which then is causing the issue you see.

Or, explicitly set the type as you found...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 1:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Or actually, all you really need to do is to tell the parser that these literals should be treated as floats:

Code:
select scan
         from com.gemex.scan.Scan as scan
        where scan.productPrefix = :productPrefix
          and scan.id != :id
          and scan.whiteLightRawValue > (:whiteLightRawValue - .0001f)
          and scan.whiteLightRawValue < (:whiteLightRawValue + .0001f)
          and scan.colorLightRawValue > (:colorLightRawValue - .0001f)
          and scan.colorLightRawValue < (:colorLightRawValue + .0001f)
          and scan.scintillationRawValue > (:scintillationRawValue - .0001f)
          and scan.scintillationRawValue < (:scintillationRawValue + .0001f)
          and (scan.scanStatusCode = :unreviewedScanStatusCode or scan.scanStatusCode = :skippedScanStatusCode)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 1:29 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Steve, please add this with a note to the migration guide. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 1:53 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
err, hold on. there is currently a bug in how float-literals are sent to the database (they go "as-is", meaning .0001f gets sent to the databse). I'll fix that for 3.1.1.


But it'll fix your parameter binding thing ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.