2010-05-26 156 views
2

我經常使用java.lang.Integer作爲主鍵。在這裏你可以看到一些代碼片段如何設置@Column註解使用的精度屬性?

@Entity 
private class Person { 

    private Integer id; 

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

    }   

} 

我需要設置其精度屬性值等於8。但是,在導出模式(Oracle)時,它無法按預期工作。

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文件輸出

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

始終我得到10。是否有一些解決方法來獲得8而不是10?

回答

2

Javadoc似乎表明該參數對Integer而言根本沒有意義。

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

所以看上去休眠是正確的,忽略它,因爲你的列類型是不是BigDecimal等,只是使持有的32位整數列。

+0

有趣的(+1),但如果我有一個BigInteger,其精度大於10,它的工作原理。我不明白爲什麼 – 2010-05-26 21:45:03

+0

不是真的令人驚訝的IMO。 Hibernate不會讓「按照你的意思工作」,即使它不是嚴格合法的,也不是JPA1規範定義的。 – Affe 2010-05-26 21:49:06

2

您可以設置columnDefinition屬性。

從Javadoc中:

columnDefinition (可選)SQL片段生成DDL 該列時使用。

@Entity 
private class Person { 

    private Integer id; 

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

    }   

} 
+0

謝謝(+1),columnDefinition的問題是因爲它是**供應商依賴**。 – 2010-05-27 21:23:24

+2

我相信Oracle中的數字和數字(這是SQL規範的一部分,因此與供應商無關)是等價的,因此您可以將其更改爲'NUMERIC(8,0)' – mtpettyp 2010-05-28 13:41:02