3

我对无法更改的表有以下 SQL 定义

CREATE TABLE network_model_property (
    id                NUMBER           NOT NULL PRIMARY KEY,
    name              VARCHAR2(100)    NOT NULL,
    ....        
    max               NUMBER(10),
    min               NUMBER(10),
    ....
)

JPA 的类定义如下

@Entity
@Table(name = "network_model_property")
@SequenceGenerator(name = "PropertySeq", sequenceName = "MODEL_PROPERTY_SEQUENCE")
public class ModelProperty implements Serializable {

    static final long serialVersionUID = -3276944825572313129L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PropertySeq")
    @Column(name = "id")
    private Long id;

    @Column(name = "max")
    private Double max;

    @Column(name = "min")
    private Double min;


    public Double getMax() {
        return max;
    }

    public void setMax(Double max) {
        this.max = max;
    }

    public Double getMin() {
        return min;
    }

    public void setMin(Double min) {
        this.min = min;
    }

我正在运行一个简单的测试用例,它在设置“hibernate.hbm2ddl.auto=validate”时抛出此异常

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: assure] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:132)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:224)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:291)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
    ... 33 more
Caused by: org.hibernate.HibernateException: Wrong column type in POCOWN1.NETWORK_MODEL_PROPERTY for column max. Found: number, expected: double precision
    at org.hibernate.mapping.Table.validateColumns(Table.java:284)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1116)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:349)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
    ... 38 more

我试过了

@Column(name = "max", columnDefinition="Decimal(10,2) default '100.00'")

@Column(name = "max",precision=10, scale=2)
4

5 回答 5

4

在您的表定义中,最大和最小列具有指定的精度,但没有比例。尝试在您的 Column JPA 定义中使用 scale = 0 作为 max & min 属性。

于 2012-04-05T13:53:14.490 回答
2

已经很长时间了,但为了完整起见,最简单的修复,它将您的 JPA 实体更改为将 min 和 max 定义为java.math.BigDecimal. java.math.BigIntegerjava.math.BigDecimal类型提供 SQLNumericNumber数据类型的直接转换。

否则,为了正确映射这些值,您需要参考 DB Driver for JDBC native type translation 来确定 jdbc 类型在 java 中被翻译成什么。

于 2014-09-15T22:49:43.230 回答
0

是的,如果您不指定比例,jpa 会将其转换为数字类型。因此,最好指定比例和精度。因此,无论何时对类进行建模,都指定比例和精度,即使比例为 0,也将其指定为 0。干杯伙伴!

于 2012-06-21T05:07:44.077 回答
0

您还需要指定精度和比例以及列定义,如下所示:

@Column(name = "max", precision = 10, scale = 0, columnDefinition = "NUMBER (10)")

private double variation;
于 2019-09-03T16:38:50.143 回答
0

在我的情况下,数据库与我的定义配合得很好,只有验证失败。您可以尝试使用“auto=none”。

于 2017-08-27T13:57:43.527 回答