代码之家  ›  专栏  ›  技术社区  ›  Arthur Ronald

如何设置@column annotation使用的精度属性?

  •  2
  • Arthur Ronald  · 技术社区  · 14 年前

    我经常使用java.lang.integer作为主键。在这里你可以看到一些代码

    @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 回复  |  直到 7 年前
        1
  •  2
  •   Affe    14 年前

    JavaDoc似乎表明参数对于整数根本没有意义。

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

    因此,似乎Hibernate忽略它是正确的,因为您的列类型不是BigDecimal等,而只是创建一个包含32位整数的列。

        2
  •  2
  •   mtpettyp    14 年前

    可以设置columnDefinition属性。

    来自javadocs:

    列定义 (可选)生成DDL时使用的SQL片段 对于专栏。

    @Entity
    private class Person {
    
        private Integer id;
    
        @Id
        @Column( columnDefinition="number(8,0)", nullable=false) 
        public Integer getId() {
    
        }        
    
    }