-->
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.  [ 4 posts ] 
Author Message
 Post subject: BigDecimal problem for DB2 and db2jcc.jar
PostPosted: Wed Aug 18, 2004 3:38 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
Hibernate version:
Hibernate2.1

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>

<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="com.merchantspace.commons.datatransfer.OrderDTO"
table="TORDER"
>
<id
name="uidPk"
type="int"
column="UID_PK"
>
<generator class="sequence">
<param name="sequence">SORDER</param>
</generator>
</id>
<property
name="creationDate"
type="java.sql.Timestamp"
column="CREATIONDATE"
not-null="true"
length="7"
/>
<property
name="email"
type="java.lang.String"
column="EMAIL"
not-null="true"
length="100"
/>
<property
name="subTotal"
type="java.math.BigDecimal"
column="SUBTOTAL"
not-null="true"
length="19"
/>
<property
name="tax"
type="java.lang.String"
column="TAX"
length="2000"
/>
<property
name="txnResponse"
type="java.lang.String"
column="TXNRESPONSE"
length="200"
/>
<property
name="authorizationCode"
type="java.lang.String"
column="AUTHORIZATIONCODE"
length="50"
/>
<property
name="confirmationCode"
type="java.lang.String"
column="CONFIRMATIONCODE"
length="50"
/>
<property
name="paymentType"
type="java.lang.String"
column="PAYMENTTYPE"
not-null="true"
length="50"
/>
<property
name="shippingType"
type="java.lang.String"
column="SHIPPINGTYPE"
length="50"
/>
<property
name="shippingCost"
type="java.math.BigDecimal"
column="SHIPPINGCOST"
not-null="true"
length="19"
/>
<property
name="customerDiscount"
type="java.math.BigDecimal"
column="CUSTOMERDISCOUNT"
length="19"
/>
<property
name="processed"
type="boolean"
column="PROCESSED"
length="1"
/>
<property
name="deleted"
type="boolean"
column="DELETED"
length="1"
/>
<property
name="expiryDate"
type="java.sql.Timestamp"
column="EXPIRYDATE"
length="7"
/>

<!-- associations -->
<!-- bi-directional many-to-one association to Taddress -->
<many-to-one
name="addressByShippingAddressUid"
class="com.merchantspace.commons.datatransfer.AddressDTO"
not-null="true"
>
<column name="SHIPPINGADDRESSUID" />
</many-to-one>
<!-- bi-directional many-to-one association to Taddress -->
<many-to-one
name="addressByBillingAddressUid"
class="com.merchantspace.commons.datatransfer.AddressDTO"
not-null="true"
>
<column name="BILLINGADDRESSUID" />
</many-to-one>
<!-- bi-directional one-to-many association to Tshipment -->
<bag
name="shipments"
lazy="true"
inverse="true"
>
<key>
<column name="ORDERID" />
</key>
<one-to-many
class="com.merchantspace.commons.datatransfer.ShipmentDTO"
/>
</bag>
<!-- bi-directional one-to-many association to Torderattribute -->
<bag
name="orderAttributes"
lazy="true"
inverse="true"
>
<key>
<column name="ORDERUID" />
</key>
<one-to-many
class="com.merchantspace.commons.datatransfer.OrderAttributeDTO"
/>
</bag>
<!-- bi-directional one-to-many association to Torderattribute -->
<bag
name="shipmentReturns"
lazy="true"
inverse="true"
>
<key>
<column name="ORDERUID" />
</key>
<one-to-many
class="com.merchantspace.commons.datatransfer.ShipmentReturnDTO"
/>
</bag>
<!-- bi-directional many-to-one association to Tmailordercustomeraccount -->
<many-to-one
name="mailOrderCustomerAccount"
class="com.merchantspace.commons.datatransfer.MailOrderCustomerAccountDTO"
not-null="true"
>
<column name="CUSTOMERUID" />
</many-to-one>
<!-- bi-directional many-to-one association to Tattributegroup -->
<many-to-one
name="attributeGroup"
class="com.merchantspace.commons.datatransfer.AttributeGroupDTO"
not-null="true"
>
<column name="ATTRIBUTEGROUPUID" />
</many-to-one>

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Transaction tx = s.beginTransaction();
OrderDTO dto = new OrderDTO();
dto.setCreationDate(new Date());
dto.setEmail("test");
dto.setSubTotal(new BigDecimal(12345));
dto.setPaymentType("test");
dto.setShippingCost(new BigDecimal(99999.99));
s.save(dto);
tx.commit();


Full stack trace of any exception that occurs:
net.sf.hibernate.JDBCException: could not insert: [com.merchantspace.commons.datatransfer.OrderDTO#10002]
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:478)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2360)
at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)
at test.main(test.java:45)
Caused by: com.ibm.db2.jcc.b.SQLException: BigDecimal conversion exception [ibm][db2][jcc][converters] The numeric literal "99999.990000000005238689482212066650390625" is not valid because its value is out of range.. See attached Throwable.
at com.ibm.db2.jcc.a.bf.a(bf.java:1179)
at com.ibm.db2.jcc.a.bi.a(bi.java:888)
at com.ibm.db2.jcc.a.bi.a(bi.java:609)
at com.ibm.db2.jcc.a.bi.a(bi.java:148)
at com.ibm.db2.jcc.a.bq.a(bq.java:173)
at com.ibm.db2.jcc.b.cf.a(cf.java:1070)
at com.ibm.db2.jcc.b.cf.a(cf.java:1368)
at com.ibm.db2.jcc.b.cf.executeUpdate(cf.java:322)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)
... 7 more

Name and version of the database you are using:
DB2 UDB 8.1, db2jcc.jar as jdbc driver


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 19, 2004 12:16 pm 
Beginner
Beginner

Joined: Sun May 09, 2004 7:18 pm
Posts: 35
Please, anyone has any idea about that? I can't save any BigDecimal to DB2.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 1:43 pm 
Newbie

Joined: Wed Mar 17, 2004 7:44 pm
Posts: 7
Location: Chicago Area, IL, US
Change
dto.setShippingCost(new BigDecimal(99999.99));
to
dto.setShippingCost(new BigDecimal("99999.99"));


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 1:48 pm 
Newbie

Joined: Wed Mar 17, 2004 7:44 pm
Posts: 7
Location: Chicago Area, IL, US
I should add the reasoning :) ... this is from the BigDecimal Javadoc:
Quote:
BigDecimal

public BigDecimal(double val)

Translates a double into a BigDecimal. The scale of the BigDecimal is the smallest value such that (10scale * val) is an integer.

Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.

The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.


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