Hibernate - 误导性地将值强制转换为整数

时间:2013-10-18 16:00:59

标签: hibernate

我有一张带汽车的桌子:

  • CarId
  • 客户ID
  • ...

映射文件如下所示:

<hibernate-mapping>
    <class name="org.project.Car" table="tblCar">
        <id name="id" column="CarId" type="int">
            <generator class="native"/>  
        </id>
        <property column="CustomerId" name="customerId" type="text"/>
        <many-to-one column="CustomerId" name="customer" class="org.project.Customer" insert="false" update="false"  fetch="select"/>
        ...
    </class>
</hibernate-mapping>

问题在于多对一列:当我删除它时,我可以加载所有汽车。当它在那里时,我收到以下错误:

INFO: could not read column value from result set: Cus2_3_0_; Value 00005-C cannot be converted to INTEGER.

,而00005-C是客户的ID。

为什么Hibernate在说文本时想要将customerId列强制转换为整数?或者我只能使用整数ID作为外键吗?

编辑: 我的客户映射大致如下所示:

<hibernate-mapping>
    <class name="org.project.Customer" table="tblCustomer">
        <id name="id" column="Id" type="integer">
            <generator class="native"/>  
        </id>
        <property column="CustomerId" name="customerId" type="text"/>
        ...
        <set name="cars" inverse="true" lazy="true" fetch="select">
            <key column="CustomerId" not-null="true"/>
            <one-to-many class="org.project.Car"/>
        </set>
    </class>
</hibernate-mapping>

2 个答案:

答案 0 :(得分:0)

不知道你的“客户”映射或表格是什么样的我猜测Hibernate默认会尝试使用customer表的“CustomerId”列。

您需要在映射中的customer表中指定KEY列。

答案 1 :(得分:0)

我发现了问题:

我必须将property-ref添加到我的Customer映射中的键中,以告诉Hibernate我使用的键不是主键而是另一个字段。这解释了为什么Hibernate想要将值转换为int:主键是一个整数。