-->
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.  [ 8 posts ] 
Author Message
 Post subject: BigDecimal Mapping question.
PostPosted: Wed Nov 29, 2006 3:32 pm 
Newbie

Joined: Tue Oct 17, 2006 10:57 am
Posts: 14
The database holds values like 100.1, 203, 209.11. I want hibernate to return the values in 999.99 format.
Basically I need 2 decimal places for this amount column.

Currently the mapping is
<property
name="correctedUnitPrice"
type="big_decimal"
column="CORRECTED_UNIT_PRICE"
not-null="false"

/>

How can I map so that hibernate returns 2 decimal places no matter whats in database column?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 29, 2006 6:03 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
well, is there any difference between BigDecimals containing 2, 2.0 and 2.00?

I'm not sure, but I believe there is no difference, and they contain the same value; your need is just a displaying problem, and must be done using e NumberFormatter (DecimalFormatter).

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 1:03 pm 
Newbie

Joined: Fri Feb 15, 2008 12:28 pm
Posts: 5
Hi there,

I would like to reactivate this topic because I seems to be more than just a formatting problem.
If I cannot rely on the fact, that hibernate provides values from db in a format I have set (e.g. 999.99), I cannot use calculating methods of BigDecimal without checking for the correct scale of the numbers.

I will try to explain it with to short examples:

Image that I put two values into Db. An amount and a taxPercentage. Leter I will retrieve them to do some calculations.

Code:
BigDecimal amount = new BigDecimal("100.00");
BigDecimal taxPercentage = new BigDecimal("27.00");

System.out.println(taxPercentage.divide(amount, BigDecimal.ROUND_HALF_UP));

The result will be 0.27 --> Thats correct for further processing!

--> Now put it into db
(...)
<-- Later get it from db

Code:
BigDecimal amount = getTaxPercentageFromDB();
BigDecimal taxPercentage = getAmountFromDB;

System.out.println(amount .divide(taxPercentage, BigDecimal.ROUND_HALF_UP));


The result will be zero! --> And thats my problem!

Has anyone an idea if there is a suitable solution?
Thanks!
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 5:18 pm 
Newbie

Joined: Wed Oct 01, 2008 5:11 pm
Posts: 2
Hi mnsweeps,

I am facing the same problem, did you find a solution?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2008 8:13 pm 
Newbie

Joined: Wed Oct 31, 2007 5:36 pm
Posts: 13
How about manipulating the POJO setter method to force a scale upon Hibernate reading from the DB?

Code:
public void setXXX(BigDecimal x) {
    if (x.scale() < 2)
        this.x = x.setScale(2);
    else
        this.x = x;
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 02, 2008 3:57 am 
Newbie

Joined: Wed Oct 01, 2008 5:11 pm
Posts: 2
Yes, this is the workaround I've been using so far.

I tried to configure hibernate mapping by setting precision and scale attributes, but it didn't work. Is it normal?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 10:36 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 9:44 am
Posts: 23
Location: Brasil
Hi, I guess the scale configuration on Hibernate mapping only works when it create the table on database based on mapping. In any way, I'm trying to mannipulate my POJO set method but it don't works! How can I tell to Hibernate use the POJO set method?

Thank you very much!!!

Ary Junior


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 10:41 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Use access-type property instead of field. If you use Annotations, annotate getters instead of fields.

_________________
-----------------
Need advanced help? http://www.viada.eu


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