openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeremy Bauer <techhu...@gmail.com>
Subject Re: Saving a double in prosgres
Date Fri, 11 Jun 2010 17:52:43 GMT
Hi Håkon,

I also see the rounded value when using DECIMAL(10, 6).  OpenJPA passes the
double value as-is to the JDBC driver and the JDBC driver or PostgreSQL DB
does the rounding.  It looks like "double precision" is a better choice for
a data type.

@ElementColumn(name = "ELEMENT", columnDefinition="double
precision",nullable=false)

Using this type, I was able to store and returned the unrounded value
[2.1999999999999993] within my entity upon query.

Note:  If you use SQL Shell/psql to verify the value, the value gets
displayed as rounded whether you use the decimal or double precision type.
So, be sure you validate the value within your JPA code.

-Jeremy

On Fri, Jun 11, 2010 at 9:00 AM, Rick Curtis <curtisr7@gmail.com> wrote:

> Are you using OpenJPA to generate your tables? If so, what is the SQL that
> is being generated to create this table?
>
> 2010/6/11 Håkon Sagehaug <hakon.sagehaug@uni.no>
>
> > Hi
> >
> > I tried to add the column definition
> >
> > @ElementColumn(name = "ELEMENT", columnDefinition="DECIMAL(10,
> > 6)",nullable=false)
> >
> > But got the same when I want to persist value 2.1999999 I end up with
> 2.2.
> > Any other tips? I use openjpa 1.2.0 should I migrate to 2.0?
> >
> > cheers, Håkon
> >
> >
> >
> > On 10 June 2010 18:19, Jeremy Bauer <techhusky@gmail.com> wrote:
> >
> > > Håkon,
> > >
> > > I did not find a DOUBLEUNSIGNED type supported for PostgreSQL, but
> could
> > > have missed something.
> > >
> > > This should provide what you are looking for though...
> > >
> > >    @PersistentCollection
> > >    @ElementColumn(name = "ELEMENT", columnDefinition="DECIMAL(10,
> > > 6)",nullable=false)
> > >    private double[] doubleValues;
> > >
> > > JPA/OpenJPA 2.0 provides standardized support for column definitions by
> > > using @ElementCollection and @Column, but unlike @PersistentCollection,
> > > @ElementCollection cannot be applied to an array.  Instead, you'd need
> to
> > > use a collection type.
> > >
> > > If you are using OpenJPA 2.0 you could modify your code as follows:
> > >
> > >    @ElementCollection
> > >    @Column(columnDefinition="DECIMAL(10,6)",nullable=false)
> > >    private List<Double> doubleValues;
> > >
> > >    public void setDoubles(Double[] doubles) {
> > >         doubleValues = Arrays.asList(doubles);
> > >    }
> > >
> > >    public Double[] getDoubles() {
> > >        return (Double[])doubleValues.toArray();
> > >    }
> > >
> > > Since List does not support primitive types, you'd also need to use the
> > > Double object type.  You could then modify your getter and setter to
> > > convert
> > > to and from a primitive array.  I didn't go that far, but it would be
> > > fairly
> > > simple.  In addition, if you need to maintain the order of the array in
> > the
> > > DB, use the @OrderColumn annotation.  There is an OpenJPA version for
> JPA
> > > 1.0 apps and a standardized version in JPA 2.0, same annotation name,
> > > different packages.
> > >
> > > hth,
> > > -Jeremy
> > >
> > > 2010/6/10 Håkon Sagehaug <hakon.sagehaug@uni.no>
> > >
> > > > Hi all,
> > > >
> > > > I've got a persitent collection in a entity looking like this
> > > >
> > > > @PersistentCollection
> > > >    double[] doubleValues;
> > > >
> > > > But when  I store the values the, it's rounded up. So if I want to
> > store
> > > > the
> > > > value 2.1999999999999993 in the database it's stored as 2.2. The
> driver
> > I
> > > > 'm
> > > > using is postgresql-8.3-603.jdbc3.jar. Should I use another type in
> the
> > > > entity or is there another solution. I guess it's this issue [1], I'm
> > > > facing. I found that you could set column property like this
> > > >
> > > >
> > > >
> > >
> >
>  @Column(columnDefinition="DOUBLEUNSIGNED",precision=10,scale=6,nullable=false)
> > > >
> > > > But hwo can I set hat on a persitent collection?
> > > >
> > > > cheers, Håkon
> > > >
> > > > [1]
> > > >
> > > >
> > >
> >
> http://openjpa.apache.org/builds/latest/docs/manual/dbsupport_postgresql.html#dbsupport_postgresql_issues
> > > >
> > >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message