休眠:发现:浮点数,预期:双精度

发布于 2024-08-27 05:56:29 字数 383 浏览 5 评论 0 原文

我在将 Oracle Float 双精度数据类型映射到 Java Double 数据类型时遇到问题。当使用 Java Double 数据类型时,hibernate 模式验证器似乎会失败。

org.hibernate.HibernateException: Wrong column type in DB.TABLE for column amount. Found: float, expected: double precision

避免这种情况的唯一方法是禁用架构验证并希望架构与即将运行的应用程序同步。在投入生产之前必须对其进行修复。

应用环境:
- Grails 1.2.1
- Hibernate核心3.3.1.GA
- 甲骨文10g

I have a problem with the mapping of Oracle Float double precision datatype to Java Double datatype. The hibernate schema validator seems to fail when the Java Double datatype is used.

org.hibernate.HibernateException: Wrong column type in DB.TABLE for column amount. Found: float, expected: double precision

The only way to avoid this is to disable schema validation and hope the schema is in sync with the app about to run. It must be fixed before it goes out to production.

App's evironment:
- Grails 1.2.1
- Hibernate-core 3.3.1.GA
- Oracle 10g

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夏有森光若流苏 2024-09-03 05:56:29

除非您在 DDL 文件中将列类型定义为双精度,否则 Oracle 会将其转换为浮点列类型。因此需要在方言类中将 double 注册为 float 列类型。

public class Oracle10gDialectExtended extends Oracle10gDialect {

    public Oracle10gDialectExtended() {
        super();
        registerColumnType(Types.DOUBLE, "float");
    }
}

最后在 Hibernate/JPA 配置中将 Oracle10gDialectExtended 注册为 hibernate.dialect

Unless you define a column type as double precision in DDL file, Oracle will convert it to float column type. So you need to register double as float column type in dialect class.

public class Oracle10gDialectExtended extends Oracle10gDialect {

    public Oracle10gDialectExtended() {
        super();
        registerColumnType(Types.DOUBLE, "float");
    }
}

Finally register Oracle10gDialectExtended in Hibernate/JPA configuration as hibernate.dialect.

猛虎独行 2024-09-03 05:56:29

需要更多信息。桌子是双人的?我对Oracle不熟悉,但那是浮点类型吗?它转换成什么 java.sql.Types 类型?您可以在数据库的方言类中看到 java.sql.Types 到数据库类型的映射。在本例中,它是 org.hibernate.dialect.Oracle10gDialect(它扩展了 9i 和 8i)。看起来你有

registerColumnType( Types.DOUBLE, "double precision" );

所以,表需要定义为双精度,并且java类需要定义为映射到Types.Double的东西,通常<代码>双

从错误消息来看,您的表似乎被定义为 float ,它将使用此映射

registerColumnType( Types.FLOAT, "float" );

,其中预期的 java 类型通常会映射到 Types.FLOAT 浮动;单个精度值。

最简单的方法是更改​​表或 java 类以匹配。或者,您可以指定将单精度值映射到双精度值的用户类型;我想不出为什么你真的想这样做,但也许如果你无法控制班级和桌子,我认为这是相当罕见的。

嗯。

Need more info. The table is Double? I'm not familiar with Oracle, but is that a floating point type? What java.sql.Types type does it translate to? You can see the java.sql.Types to database type mapping in the dialect class for your database. In this case it is org.hibernate.dialect.Oracle10gDialect (which extends 9i and 8i). Looks like you have

registerColumnType( Types.DOUBLE, "double precision" );

So, the table needs to be defined as double precision, and the java class needs to be defined as something that will map to Types.Double, usually double.

From the error message, it looks like your table is defined as float which would use this mapping

registerColumnType( Types.FLOAT, "float" );

for which the expected java type would be something that maps to Types.FLOAT, usually float; a single precision value.

The easiest thing to do is either change your table or java class to match. Alternately, you can specify a user type that would map a single precision value to a double precision value; I can't think of why you would really want to do that, but maybe if you didn't have control over both the class and the table, which is quite rare, I would think.

hth.

关于从前 2024-09-03 05:56:29

如果您使用注释方法,则需要为字段声明列类型,如下所示。

@Column(name = "PERFORMANCE", columnDefinition = "FLOAT(5,2)")
private double performance;

If you are using Annotation method, you need to declare column type as below for the field.

@Column(name = "PERFORMANCE", columnDefinition = "FLOAT(5,2)")
private double performance;
瑾兮 2024-09-03 05:56:29

我们有同样的问题。我们通过确保在映射中明确设置列类型来解决这个问题,如下所示:

<property name="performance" type="double">
  <column name="PERFORMANCE" sql-type="float" />
</property>

We had the same issue. We solved it by making sure to explicity set the column type in the mapping, like this:

<property name="performance" type="double">
  <column name="PERFORMANCE" sql-type="float" />
</property>
陪你到最终 2024-09-03 05:56:29

应用程序属性

hibernate.dialect=com.test.config.Oracle10gDialectExtended

创建类

package com.test.config;

import java.sql.Types;

import org.hibernate.dialect.Oracle10gDialect;

public class Oracle10gDialectExtended extends Oracle10gDialect {

    public Oracle10gDialectExtended() {
        registerColumnType(Types.DOUBLE, "float");
    }
}

application.properties

hibernate.dialect=com.test.config.Oracle10gDialectExtended

create class

package com.test.config;

import java.sql.Types;

import org.hibernate.dialect.Oracle10gDialect;

public class Oracle10gDialectExtended extends Oracle10gDialect {

    public Oracle10gDialectExtended() {
        registerColumnType(Types.DOUBLE, "float");
    }
}
等待圉鍢 2024-09-03 05:56:29

我遇到过这样的情况:我的 Oracle 列类型为 NUMBER,而 Java 类型为 double

最后发现它是由于设置

validate

只是将其更改为

一切正常!

I had a case where my Oracle column type was NUMBER and the Java type was double.

Finally traced that it was down to settings

<property name="hbm2ddl.auto">validate</property>

just changed it to

<property name="hbm2ddl.auto">update</property>

and everything worked!

嘿咻 2024-09-03 05:56:29

经过大量的研究和尝试,我找到了解决方案。对于 Grails 域,我们可以通过将 sqlType 添加为“float” 作为验证模式来解决此问题。

在 Result.groovy 中

class Result{
    Double discount

    static mapping = {
        discount column: "discount", sqlType: "float"
    }

    static constraints = {
        discount(nullable: true)
    }
}

,它会消除验证模式下的错误。

After lots of research and tries i found the solution of it. For Grails domain we can fix this problem by adding the sqlType as "float" for validation mode.

In Result.groovy

class Result{
    Double discount

    static mapping = {
        discount column: "discount", sqlType: "float"
    }

    static constraints = {
        discount(nullable: true)
    }
}

So it will remove the error in validation mode.

梦毁影碎の 2024-09-03 05:56:29

我在将 jboss 4/hibernate 3 应用程序迁移到 as7/hibernate4 时遇到了这个问题。 Rob Keilty 建议将 hbm2ddl.auto 从验证更改为更新,而无需更改旧代码,从而解决了问题。

I had this issue when migrating a jboss 4/hibernate 3 app to as7/hibernate4. Problem solved by Rob Keilty's suggestion of changing hbm2ddl.auto from validate to update without having to change legacy code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文