[英]How to set up precision attribute used by @Column annotation?
I often use java.lang.Integer as primary key. 我经常使用java.lang.Integer作为主键。 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 . 我需要将其精度属性值设置为8 。 But, when exporting The schema (Oracle), it does not work as expected.
但是,导出架构(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 outputs schema.sql输出
create table Person (id number(10,0) not null)
Always i get 10 . 我总是得到10 。 Is there some workaround to get 8 instead of 10?
有一些解决方法可以使8代替10?
Javadoc seems to indicate that parameter isn't meaningful at all for Integer. Javadoc似乎表明该参数对Integer毫无意义。
http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html#precision%28%29 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. 因此,休眠状态似乎可以忽略它,因为您的列类型不是BigDecimal等,而只需创建一个包含32位整数的列即可。
You could set the columnDefinition attribute. 您可以设置columnDefinition属性。
From the Javadocs: 从Javadocs:
columnDefinition (Optional) The SQL fragment that is used when generating the DDL for the column.
columnDefinition(可选)在为列生成DDL时使用的SQL片段。
@Entity
private class Person {
private Integer id;
@Id
@Column( columnDefinition="number(8,0)", nullable=false)
public Integer getId() {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.