I am using hibernate v3.1 and MySQL4 on linux. I have come to a problem with querrying/comparing floats.
A 3rd party component has put a value, for example 4.4 into table with float column with defined precision (FLOAT(4,2)). The DB will not store the float exactly as 4.4, the float is always stored in system dependent format (probably as 4.400000001 or 4.3999999999). By running a general querry I get proper result:
Code:
select * from ttt;
score
4.4
However if I explicitely ask for the value with
WHERE caluse I get nothing:
Code:
select * from ttt where score=4.4;
Empty set
More of this issue can be found in MySQL documentation
http://dev.mysql.com/doc/refman/4.1/en/problems-with-float.htmlIn simple it can be handled by round construct
Code:
select * from ttt where round(score,2)=round(4.4,2);
score
4.4
I hoped I will handle this problem by specifying
precision and
scale attributes in Hibernate mapping file (
<property name="score" column="`score`" type="float" not-null="false" precision="4" scale="2"/>) and then create
Criterion object like
"Restrictions.eq("score", new Float(4.4))".
But there is no visible effect on querry. With or without
precision and
scale attributes the querry created by
Criteria class looks always the same (score=4.4).
I am looking for some way how to force hibernate to use
round function in
WHERE clause or any similar method when specifing the filter criteria for a column. Can anyone help me? Any idea what to do? Thanks in advance