You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Jacek Laskowski <ja...@laskowski.net.pl> on 2007/04/10 19:24:42 UTC

@Column with precision and scale - how does it work?

Hi,

I wonder how I could restrict what values are inserted into a table
using @Column(precision=5, scale=2). When does it matter?

I'm using OpenJPA 0.9.7-SNAPSHOT with Derby and with the following:

    @Column(precision = 5, scale = 2)
    public double getPensja() {
        return pensja;
    }

OpenJPA executes the following CREATE

2969  derbyPU  TRACE  [main] openjpa.jdbc.SQL - <t 11533424, conn
18662247> executing stmnt 23119024 CREATE TABLE Osoba (numer BIGINT
NOT NULL, dzienImienin TIMESTAMP, dzienUrodzin
 TIMESTAMP, imie VARCHAR(255), kraj VARCHAR(255), nazwisko
VARCHAR(255), wersja INTEGER, pensja DOUBLE, tytul VARCHAR(255),
PRIMARY KEY (numer))

How could I restrict the precision and scale of the pensja field? Is
the columnDefinition attribute of @Column the last resort? When is the
others used? What databases are supported?

Jacek

-- 
Jacek Laskowski
http://www.JacekLaskowski.pl

Re: @Column with precision and scale - how does it work?

Posted by Jacek Laskowski <ja...@laskowski.net.pl>.
On 4/10/07, Michael Dick <mi...@gmail.com> wrote:

> Would you mind opening a JIRA report for the problem?
> http://issues.apache.org/jira/browse/OPENJPA

https://issues.apache.org/jira/browse/OPENJPA-213

Jacek

-- 
Jacek Laskowski
http://www.JacekLaskowski.pl

Re: @Column with precision and scale - how does it work?

Posted by Craig L Russell <Cr...@Sun.COM>.
According to the Persistence specification, the @Column annotation says:

int precision (Optional) The precision for a decimal (exact
numeric)column.(Appliesonlyifadecimalcolumn is used.)
0 (Value must be set by
developer.)
int scale (Optional) The scale for a decimal (exact
numeric)column.(Appliesonlyifadecimalcolumn is used.)
0
[sic]

So unless you are using an exact numeric type for your column  
(NUMERIC or DECIMAL) a JPA provider should simply ignore the type.

The intent of this part of the specification is not to provide some  
field-level behavior, for example to convert the data on its way to  
or from the database, but simply to give the provider some  
information that would allow it to create a reasonable column type  
for schema creation.

Craig

On Apr 10, 2007, at 2:59 PM, Michael Dick wrote:

> I'm sorry, I misunderstood your question.
>
> I'm afraid I don't know how we determine when to apply the scale and
> precision.
>
> In this case the value should be ignored, since there's no way to  
> set it on
> the column. If the attribute was of type BigDecimal then I think the
> precision and scale should apply.
>
> The catch here is that it looks like we're mapping BigDecimal to  
> DOUBLE so
> that won't work. I'll have to take a closer look to determine where  
> that
> mapping occurs, and what the correct mapping(s) should be.
>
> Would you mind opening a JIRA report for the problem?
> http://issues.apache.org/jira/browse/OPENJPA
>
> -Mike
>
> On 4/10/07, Jacek Laskowski <ja...@laskowski.net.pl> wrote:
>>
>> On 4/10/07, Michael Dick <mi...@gmail.com> wrote:
>> > I don't think Derby supports specifying the precision on type  
>> DOUBLE
>> (maybe
>> > other datbases do).
>>
>> You're right - it doesn't.
>>
>> > You should be able to specify the precision through the @Column
>> annotation.
>> > I believe @Column(columnDefinition="DECIMAL(5,2)") will
>> > work.
>>
>> DECIMAL is a synonim of NUMERIC and either works well.
>>
>> > I'm not sure whether precision=x, scale=y with a type that maps to
>> > DECIMAL instead of DOUBLE.
>>
>> That's my question how OpenJPA recognizes whether the attributes
>> should be used or not. I don't think precision and scale are not used
>> at all.
>>
>> Jacek
>>
>> --
>> Jacek Laskowski
>> http://www.JacekLaskowski.pl

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Re: @Column with precision and scale - how does it work?

Posted by Michael Dick <mi...@gmail.com>.
I'm sorry, I misunderstood your question.

I'm afraid I don't know how we determine when to apply the scale and
precision.

In this case the value should be ignored, since there's no way to set it on
the column. If the attribute was of type BigDecimal then I think the
precision and scale should apply.

The catch here is that it looks like we're mapping BigDecimal to DOUBLE so
that won't work. I'll have to take a closer look to determine where that
mapping occurs, and what the correct mapping(s) should be.

Would you mind opening a JIRA report for the problem?
http://issues.apache.org/jira/browse/OPENJPA

-Mike

On 4/10/07, Jacek Laskowski <ja...@laskowski.net.pl> wrote:
>
> On 4/10/07, Michael Dick <mi...@gmail.com> wrote:
> > I don't think Derby supports specifying the precision on type DOUBLE
> (maybe
> > other datbases do).
>
> You're right - it doesn't.
>
> > You should be able to specify the precision through the @Column
> annotation.
> > I believe @Column(columnDefinition="DECIMAL(5,2)") will
> > work.
>
> DECIMAL is a synonim of NUMERIC and either works well.
>
> > I'm not sure whether precision=x, scale=y with a type that maps to
> > DECIMAL instead of DOUBLE.
>
> That's my question how OpenJPA recognizes whether the attributes
> should be used or not. I don't think precision and scale are not used
> at all.
>
> Jacek
>
> --
> Jacek Laskowski
> http://www.JacekLaskowski.pl

Re: @Column with precision and scale - how does it work?

Posted by Jacek Laskowski <ja...@laskowski.net.pl>.
On 4/10/07, Michael Dick <mi...@gmail.com> wrote:
> I don't think Derby supports specifying the precision on type DOUBLE (maybe
> other datbases do).

You're right - it doesn't.

> You should be able to specify the precision through the @Column annotation.
> I believe @Column(columnDefinition="DECIMAL(5,2)") will
> work.

DECIMAL is a synonim of NUMERIC and either works well.

> I'm not sure whether precision=x, scale=y with a type that maps to
> DECIMAL instead of DOUBLE.

That's my question how OpenJPA recognizes whether the attributes
should be used or not. I don't think precision and scale are not used
at all.

Jacek

-- 
Jacek Laskowski
http://www.JacekLaskowski.pl

Re: @Column with precision and scale - how does it work?

Posted by Michael Dick <mi...@gmail.com>.
I don't think Derby supports specifying the precision on type DOUBLE (maybe
other datbases do).

You should be able to specify the precision through the @Column annotation.
I believe @Column(columnDefinition="DECIMAL(5,2)") will work. I'm not sure
whether precision=x, scale=y with a type that maps to DECIMAL instead of
DOUBLE.


On 4/10/07, Jacek Laskowski < jacek@laskowski.net.pl> wrote:
>
> Hi,
>
> I wonder how I could restrict what values are inserted into a table
> using @Column(precision=5, scale=2). When does it matter?
>
> I'm using OpenJPA 0.9.7-SNAPSHOT with Derby and with the following:
>
>     @Column(precision = 5, scale = 2)
>     public double getPensja() {
>         return pensja;
>     }
>
> OpenJPA executes the following CREATE
>
> 2969  derbyPU  TRACE  [main] openjpa.jdbc.SQL - <t 11533424, conn
> 18662247> executing stmnt 23119024 CREATE TABLE Osoba (numer BIGINT
> NOT NULL, dzienImienin TIMESTAMP, dzienUrodzin
> TIMESTAMP, imie VARCHAR(255), kraj VARCHAR(255), nazwisko
> VARCHAR(255), wersja INTEGER, pensja DOUBLE, tytul VARCHAR(255),
> PRIMARY KEY (numer))
>
> How could I restrict the precision and scale of the pensja field? Is
> the columnDefinition attribute of @Column the last resort? When is the
> others used? What databases are supported?
>
> Jacek
>
> --
> Jacek Laskowski
> http://www.JacekLaskowski.pl
>



-- 
-Michael Dick