HSQL + Hibernate异常:错误的列类型:找到:double,expected:float [英] HSQL + Hibernate Exception: Wrong column type: Found: double, expected: float

查看:148
本文介绍了HSQL + Hibernate异常:错误的列类型:找到:double,expected:float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Hibernate中使用内存中的HSQL(HSQLDB)来处理所有的单元测试,因为它速度非常快。我有一个列定义如下的表:

  float qw; 

当hibernate启动时,出现以下错误:

  org.hibernate.HibernateException:MyTable中qw列的列类型错误。 
找到:double,expected:float

为什么找到 double 当列被声明为 float

解决方案

这是由于一系列不幸事件造成的。


  1. 问题始于HSQLDB不支持
    float 数据类型。 (Duh?是的,我知道,但是文档
    在这里
    。 )

  2. 这个问题开始变得丑陋,因为HSQLDB不仅仅是简单的 fail strong>当你指定一个 float 列时,但它
    静静地将它重新解释为 double ,假装到
    毫无戒心的程序员,一切都很顺利。因此,在您的
    create table 语句中,您可以将列的类型指定为
    float ,并且HSQLDB将会成功,但它只是在拖动你
    ,因为如果你稍后查询那个列的类型,你会发现
    它是 double ,而不是 float


  3. 然后,hibernate发现此列为 double ,而
    期望它是 float ,并且它不够聪明以利用事实
    float 可从 double 赋值。大家都知道 double
    float 要好,所以
    hibernate实际上应该是 happy 它发现 double ,而
    只需要一个 float , 对? - 但不行,hibernate不会有
    中的任何一个:当它期望一个 float 时,除了 float 会做。

  4. 然后,有一个关于hibernate的有趣的事情,据推测它有
    内置的对HSQLDB的支持,事实上,它包括一个
    类org.hibernate.dialect.HSQLDialect
    但是 方言不关心你的浮动。 因此,他们不认为数据类型不兼容是一个方言问题?他们
    从来没有用浮游物测试过它?我不知道该假设什么,但是
    的真相是HSQLDB的hibernate方言
    没有为这个问题提供任何修正。


    那么,我们可以做什么?

    这个问题的一个可能的解决方案是创建我们自己的hibernate我们纠正了这种差异。

    在过去,我遇到了一个类似的问题,MySQL和 boolean vs bit ,(请参阅此问题:" Found:bit,希望:boolean在Hibernate 4升级后))所以对于HSQLDB我解决了 float double 通过为hibernate声明自己的HSQLDB方言:

      / ** 
    *'修正'HSQL方言。
    *
    * PEARL:HSQL似乎遇到了浮动问题。我们在这里补救。
    *见https://stackoverflow.com/q/28480714/773113
    *
    * PEARL:这个类必须是公共的,而不是包私有的,它必须有一个
    * public构造函数,否则hibernate将无法实例化它。
    * /
    public class FixedHsqlDialect extends HSQLDialect
    {
    public FixedHsqlDialect()
    {
    registerColumnType(java.sql.Types.FLOAT,double );
    }
    }

    并如下使用它:

      ejb3cfg.setProperty(hibernate.dialect,FixedHsqlDialect.class.getName()); 
    //而不是:org.hibernate.dialect.HSQLDialect.class.getName();


    I am using in-memory HSQL (HSQLDB) with Hibernate for all my unit tests, because it is very fast. I have a table with a column defined as follows:

    float qw;
    

    When hibernate starts, I get the following error:

    org.hibernate.HibernateException: Wrong column type in MyTable for column qw. 
    Found: double, expected: float
    

    Why does it find double when the column is declared as float?

    This is happening due to a series of unfortunate events.

    1. The problem begins with the fact that HSQLDB does not support the float data type. (Duh? Yes, I know, but Documentation here.)

    2. The problem starts becoming ugly due to the fact that HSQLDB does not simply fail when you specify a float column, but it silently re-interprets it as double, pretending to the unsuspecting programmer that everything went fine. So, in your create table statement you may specify the type of a column as float, and HSQLDB will succeed, but it is only trolling you, because if you later query the type of that column, you will find that it is double, not float.

    3. Then, later, hibernate finds this column to be double, while it expects it to be float, and it is not smart enough to make use of the fact that float is assignable from double. Everyone knows that a double is better than a float, so hibernate should actually be happy that it found a double while all it needed was a float, right? --but no, hibernate will not have any of that: when it expects a float, nothing but a float will do.

    4. Then, there is this funny thing about hibernate supposedly having built-in support for HSQLDB, as evidenced by the fact that it includes a class org.hibernate.dialect.HSQLDialect, but the dialect does not care about your floats. So, they don't believe that a data type incompatibility is a dialect issue? they never tested it with floats? I don't know what to suppose, but the truth of the matter is that the hibernate dialect for HSQLDB does not provide any correction for this problem.

    So, what can we do?

    One possible solution to the problem is to create our own hibernate dialect for HSQLDB, in which we correct this discrepancy.

    In the past I came across a similar problem with MySQL and boolean vs. bit, (see this question: "Found: bit, expected: boolean" after Hibernate 4 upgrade) so for HSQLDB I solved the problem with float vs. double by declaring my own HSQLDB dialect for hibernate:

    /**
     * 'Fixed' HSQL Dialect.
     *
     * PEARL: HSQL seems to have a problem with floats.  We remedy this here.
     * See https://stackoverflow.com/q/28480714/773113
     *
     * PEARL: this class must be public, not package-private, and it must have a 
     * public constructor, otherwise hibernate won't be able to instantiate it.
     */
    public class FixedHsqlDialect extends HSQLDialect
    {
        public FixedHsqlDialect()
        {
            registerColumnType( java.sql.Types.FLOAT, "double" );
        }
    }
    

    And using it as follows:

    ejb3cfg.setProperty( "hibernate.dialect", FixedHsqlDialect.class.getName() );
        //Instead of: org.hibernate.dialect.HSQLDialect.class.getName();
    

    这篇关于HSQL + Hibernate异常:错误的列类型:找到:double,expected:float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆