-->
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.  [ 14 posts ] 
Author Message
 Post subject: Lost precision
PostPosted: Wed Oct 19, 2005 11:27 am 
Newbie

Joined: Wed Oct 19, 2005 11:09 am
Posts: 2
Hi There...

I am on a situation that when saving data using hibernate I am loosing precision. I'm trying to save the value 99,999,999.99 but it keeps saving 100,000,000 at database.

I changed mapping from double to BigDecimal, changed the bean also, but it did not helped too much. I've made the scale precision at code, but it did not solved too.

I am using hibernate 2, DB2 7.1.

The collumn type at DB2 is decimal (15,2).

The code saving data is:
Code:
   session = getSessionFactory().openSession();
   transaction = session.beginTransaction();

   session.save(bean.getShipping());
   session.save(bean.getBilling());
   session.save(bean);
   transaction.commit();


I've tried a lot of solutions but hibernate keeps rounding my numbers.

The collumns at mapping are like this :

Code:
<property name="totalPrice" column="TOTAL_PRICE" type="big_decimal"  length="2"/>
<property name="subTotal" column="SUB_TOTAL" type="big_decimal" length="2"/>


The length="2" I've just put now for testing purpose but it did not chaged the behavior.

Appreciate any help!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 12:04 pm 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
Can you try precision="30" scale="2" in property?

I couldnt find enough documentation on Precision/Scale/Length attributes of property tag in hibernate reference or in hibernate in action book.

I am also facing similar problem.

I am using Hibernate3.0 with MS SQL DB(jTDS Microsoft SQL Driver). Here is the problem,

for Example,

If I have this table,

create table Test(floatColumn FLOAT)

and mapping

<property name="floatColumn">

If I try to save 100.60 into the column, my DB Shows 100.599609375.

I tried with,
<property name="floatColumn" scale="2">, <property name="floatColumn" scale="2" precision="30"> and ofcourse <property name="floatColumn" precision="2">. I have the same result !

Anybody has some clue? or reference to documentation?

thx
ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 1:48 pm 
Newbie

Joined: Wed Oct 19, 2005 11:09 am
Posts: 2
I cannot use precision because it is from hibernate 3.x and I am using the old versio, 2.x

Any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 3:11 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Have you tried any of this with Java and JDBC ? I'd bet you'll find you'll get similar results.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 3:20 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Note that the following code prints

100,000,000
$100,000,000.00


Float f1 = new Float(99999999.99);
NumberFormat nf1 = DecimalFormat.getNumberInstance();
NumberFormat nf2 = DecimalFormat.getCurrencyInstance();
System.out.println(nf1.format(f1));
System.out.println(nf2.format(f1));

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 4:33 am 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
@pksiv,

You are right.. JDBC too modifying value... so it isnt hibernate problem !!.. good to know..

I ll start looking into JDBC API for this problem..

thnx
ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:09 am 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
I guess I found a way out,

The problem is with how Java Stores/Retrieves Floating Point Variables/Constants.


Best thing to do now is, use String type in hibernate mapping. like,

Code:
<property name="floatColum" type="string"/>


and ofcourse your POJO should contain String floatColumn not float floatColumn.

It is working for me with Hibernate3.0, MS SQL. let us know if its working for you too..


ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:16 am 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
I guess I found a way out,

The problem is with how Java Stores/Retrieves Floating Point Variables/Constants.


Best thing to do now is, use String type in hibernate mapping. like,

Code:
<property name="floatColum" type="string"/>


and ofcourse your POJO should contain String floatColumn not float floatColumn.

It is working for me with Hibernate3.0, MS SQL. let us know if its working for you too..


ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:24 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Just use java.math.BigDecimal in JAVA and DECIMAL in DB.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:48 am 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
@baliukas

yeah, only if he is allowed/wanted to change schema !!..

BigDecimal in Java and decimal in DB stores correctly? is it working for you?

ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:48 am 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
@baliukas

yeah, only if he is allowed/wanted to change schema !!..

BigDecimal in Java and decimal in DB stores correctly? is it working for you?

ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:49 am 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
@baliukas

yeah, only if he is allowed/wanted to change schema !!..

BigDecimal in Java and decimal in DB stores correctly? is it working for you?

ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 8:51 am 
Newbie

Joined: Wed Oct 19, 2005 11:36 am
Posts: 8
Sorry for duplicates... New here... I just went back in my browser... its creating new once...

ARMR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 9:34 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
armr wrote:
@baliukas

yeah, only if he is allowed/wanted to change schema !!..

BigDecimal in Java and decimal in DB stores correctly? is it working for you?

ARMR

Yes, it works without problems if your JDBC driver is not broken. You can test it using
"ResultSet.getBigDecimal(index)"
or
"ResultSet.getObject(index,Types.DECIMAL)"

(BigDecimal can be mapped to "NUMBER" or "NUMERIC" too).


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