views:

34

answers:

2

I often use java.lang.Integer as primary key. Here you can see some piece of code

@Entity
private class Person {

    private Integer id;

    @Id
    @Column(precision=8, nullable=false) 
    public Integer getId() {

    }        

}

I need to set up its precision attribute value equal to 8. But, when exporting The schema (Oracle), it does not work as expected.

AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration
    .addAnnotatedClass(Person.class)
    .setProperty(Environment.DIALECT, "org.hibernate.dialect.OracleDialect")
    .setProperty(Environment.DRIVER, "oracle.jdbc.driver.OracleDriver");

SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");

schema.create(true, false);

schema.sql outputs

create table Person (id number(10,0) not null)

Always i get 10. Is There some workaround to get 8 instead of 10 ?

+1  A: 

Javadoc seems to indicate that parameter isn't meaningful at all for Integer.

http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html#precision%28%29

So it would appear hibernate is right to ignore it, as your column type isn't a BigDecimal etc, and just make a column that holds a 32-bit integer.

Affe
@Affe Interesting (+1) But if i have a BigInteger whose precision is greater than 10, it works. I do not understanding why
Arthur Ronald F D Garcia
Not really surprising imo. Wouldn't be the only place Hibernate makes something "work the way you meant" even if it isn't strictly legal or well defined by the JPA1 spec.
Affe
+1  A: 

You could set the columnDefinition attribute.

From the Javadocs:

columnDefinition (Optional) The SQL fragment that is used when generating the DDL for the column.

@Entity
private class Person {

    private Integer id;

    @Id
    @Column( columnDefinition="number(8,0)", nullable=false) 
    public Integer getId() {

    }        

}
mtpettyp
Thank you (+1), The problem with columnDefinition is because it is **vendor-dependent**.
Arthur Ronald F D Garcia
I believe that number and numeric (which is part of the SQL spec and thus vendor-independent) are equivalent in Oracle so you could change it to `NUMERIC(8,0)`
mtpettyp