2

我尝试使用此 xml 为具有双数据类型的休眠分配 ID

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping >
        <class name="User" table="user">
            <id name="id" type="double"  >
                <column name="id_user" />
                <generator class="increment"  />
            </id>
            <property name="username" />
            <property name="password" />
            <property name="email" />

       </class>
    </hibernate-mapping>

但它给了我错误

org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.Double
    at org.hibernate.id.IdentifierGeneratorHelper.getIntegralDataTypeHolder(IdentifierGeneratorHelper.java:210) at org.hibernate.id.IdentifierGeneratorHelper.getIntegralDataTypeHolder(IdentifierGeneratorHelper.java:210)

我的代码有什么问题?或者它是一个休眠错误或什么?

4

1 回答 1

5

生成 id 应该很简单,使用双精度的 id 会在不需要的地方引入复杂性。Hibernate 告诉你不要对 id 使用双精度,使用整数类型(或 UUID 或字符串)。基本上,您想使用比较简单的东西(它们不是浮点数,请参阅这个问题以了解比较多毛的情况)。使用浮点数,整数值之间的比较是最直接的,但是如果您不希望小数点后有数字,为什么还要使用浮点数呢?

您在映射中选择的类型取决于数据库中使用的类型。如果您查看您的架构,通常 id 的类型是一个固定的十进制数字,小数位数为零(意味着数字不能有小数部分),因为简单的整数类型可能不足以容纳 DBA 的插入数量预计。由于它是固定十进制 JDBC 将允许将 id 映射到浮点类型,但这不是您想要的。

Fixed decimal and floating point are two different things. Fixed decimal means you have a fixed number of digits, where precision tells you how many digits before the decimal and scale tells you how many digits after the decimal point. Floating point means representing numbers using a complex binary representation (with strange things going on such as binary representation of some fractions resulting in repeating decimals that get truncated) that is a lot more complicated than what an id field needs.

于 2015-07-09T13:10:27.020 回答