简体   繁体   English

如何设置@Column批注使用的precision属性?

[英]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.

相关问题 如何以编程方式设置注释属性/属性? - How to set annotation property/attribute programmatically? Java:如何将默认值设置为注释,并将另一个注释作为其属性? - Java: how to set default value to annotation with another annotation as its attribute? 如何获取用于函数及其类的注释类型的所有注释属性? - How to get all annotation's attribute for an annotation type used for function and its class? 如何根据 Spring Boot 中的活动配置文件设置注释属性值? - How to set annotation attribute value based on the active profile in Spring Boot? 如何通过注释注入对象并在此对象上设置属性值 - How to inject an object via annotation and set an attribute value on this object 如何使用属性中的Spring @Value来设置注释属性 - How to use a Spring @Value from properties to set an annotation attribute 杰克逊,如何设置才能使用? - Jackson, how to set up so could be used? 我如何在列注释中设置默认值 - how do i set default value in column annotation 如何在Java中自动设置精度 - How to set precision automatically in java 在hibernate中避免@Column注解中的“ columnDefinition”属性 - Avoiding “columnDefinition” attribute in @Column annotation in hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM