 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Thu Aug 03, 2006 8:32 pm Post subject: Hibernate appending changing the value of doubles |
|
|
When I save some numbers (i.e. 5987.24) hibernate saves it to my
database correctly (SQL Server) but when it retrives it, it's changing
the value slightly (5987.240234375).
I'm using hibernate 3 on Windows XP Pro.
I've queried the database in between saves and retrives and verified
the number is saved correctly. I have no idea how this is happening.
When I used 5987.9 hibernate came back with a slightly lower value;
5987.899......
Using 12.0 hibernate worked correctly; it returned 12.0.
-------- from TruckLoadout.hbm.xml ------------
<property name="grossPounds" type="java.lang.Double">
<column name="GrossPounds" precision="12" scale="2" />
</property>
<property name="netPounds" type="java.lang.Double">
<column name="NetPounds" precision="12" />
</property>
--------------------------------------------------------------
I added the scale attribute to gross pounds. No effect. Apparently it's
only used by tools to build a database from the xml anyway.
Both netPounds and gross pounds have a precision of 12 and scale of 2
in my database.
I'm using the JTDS driver.
------------------- The code used to retrieve the
data-----------------------------
public TruckLoadout read(Integer equipmentId) {
TruckLoadout result = new TruckLoadout();
try {
org.hibernate.Session hibernateSession =
HibernateSessionFactory.currentSession();
Criteria criteria =
hibernateSession.createCriteria(TruckLoadout.class);
Criterion fieldName = Expression.eq("equipmentId", equipmentId);
criteria.add(fieldName);
result = (TruckLoadout)criteria.uniqueResult();
}
catch(Exception ex) {
ex.printStackTrace();
}
finally {
//Close the session
try {
HibernateSessionFactory.closeSession();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return result;
}
----------------------------------------------------------------------------------------
Again, I've looked at the data between the time hibernate stores it in
the database and when it's retrived and it is stored correctly (without
the long drawn out digits).
-------------------- From AbstractTruckLoadout.java
--------------------------
private Double grossPounds;
private Double netPounds;
/** full constructor */
public AbstractTruckLoadout(Date arrivalDt, Date startLoadingDt,
Date endLoadingDt, Double grossPounds, Double netPounds, Double
tarePounds, Integer consigneeId, Integer equipmentId) {
this.arrivalDt = arrivalDt;
this.startLoadingDt = startLoadingDt;
this.endLoadingDt = endLoadingDt;
this.grossPounds = grossPounds;
this.netPounds = netPounds;
this.tarePounds = tarePounds;
this.consigneeId = consigneeId;
this.equipmentId = equipmentId;
}
public Double getGrossPounds() {
return this.grossPounds;
}
public void setGrossPounds(Double grossPounds) {
this.grossPounds = grossPounds;
}
public Double getNetPounds() {
return this.netPounds;
}
public void setNetPounds(Double netPounds) {
this.netPounds = netPounds;
} |
|
| Back to top |
|
 |
Lee Fesperman Guest
|
Posted: Thu Aug 03, 2006 11:45 pm Post subject: Re: Hibernate appending changing the value of doubles |
|
|
KevinLEdwards (AT) gmail (DOT) com wrote:
| Quote: |
When I save some numbers (i.e. 5987.24) hibernate saves it to my
database correctly (SQL Server) but when it retrives it, it's changing
the value slightly (5987.240234375).
I'm using hibernate 3 on Windows XP Pro.
I've queried the database in between saves and retrives and verified
the number is saved correctly. I have no idea how this is happening.
When I used 5987.9 hibernate came back with a slightly lower value;
5987.899......
Using 12.0 hibernate worked correctly; it returned 12.0.
|
That's just floating-point approximation, not Hibernate. Binary floating-point can't
represent values like 5987.24 and 5987.9; it can only approximate those values. OTOH, it
can exactly represent values like 5987.5, 5987.125 and 12.0. Another choice is to use
BigDecimal, which can handle exact decimals.
--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com) |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 04, 2006 9:23 pm Post subject: Re: Hibernate appending changing the value of doubles |
|
|
Thank you Lee, that did it.
Lee Fesperman wrote:
| Quote: | KevinLEdwards (AT) gmail (DOT) com wrote:
When I save some numbers (i.e. 5987.24) hibernate saves it to my
database correctly (SQL Server) but when it retrives it, it's changing
the value slightly (5987.240234375).
I'm using hibernate 3 on Windows XP Pro.
I've queried the database in between saves and retrives and verified
the number is saved correctly. I have no idea how this is happening.
When I used 5987.9 hibernate came back with a slightly lower value;
5987.899......
Using 12.0 hibernate worked correctly; it returned 12.0.
That's just floating-point approximation, not Hibernate. Binary floating-point can't
represent values like 5987.24 and 5987.9; it can only approximate those values. OTOH, it
can exactly represent values like 5987.5, 5987.125 and 12.0. Another choice is to use
BigDecimal, which can handle exact decimals.
--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com) |
|
|
| Back to top |
|
 |
|
|
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 You cannot vote in polls in this forum
|
|