users@glassfish.java.net

BigDecimal persistence problem

From: <glassfish_at_javadesktop.org>
Date: Wed, 02 Jan 2008 01:51:56 PST

Happy New Year folks. ;-)


I think I've run into a bug.

I have an entity bean which has a BigDecimal value mapping to a double precision column in my database.

When the database record has a value of say 0.1 the entity bean loads
0.1000000000000000055511151231257827021181583404541015625
not exactly right.


Which seems to me that the persistence engine (TopLink?) is doing something like
BigDecimal value = new BigDecimal(resultSet.getDouble("qty"));
where it should be doing
BigDecimal value = BigDecimal.valueOf(resultSet.getDouble("qty"));


Is the current behaviour correct? (I can't think of any circumstances where what it is doing would be useful)


[code]
class MyEntity {
    Integer id;
    BigDecimal qty;
}

CREATE TABLE MyEntity
(
  id integer NOT NULL,
  qty double precision NOT NULL
);


    private static void test6() {
        double d = 0.1;
        BigDecimal bdBad = new BigDecimal(d);
        BigDecimal bdGood = BigDecimal.valueOf(d);
        
        System.out.println("bdBad=" + bdBad);
        System.out.println("bdGood=" + bdGood);
        System.out.println(bdBad.doubleValue());
        System.out.println(bdGood.doubleValue());
    }


[/code]
[Message sent by forum member 'phenderson' (phenderson)]

http://forums.java.net/jive/thread.jspa?messageID=251872