0

I have an table with cars:

  • CarId
  • CustomerId
  • ...

The mapping file looks like this:

<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>

The problem lies in the many-to-one column: When I remove it, I can load all the cars. When it's there, I get the following error:

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

, whereas 00005-C is the Id of a customer.

Why on earth does Hibernate want to cast the customerId column to integer when it says text? Or can I only use integer IDs for foreign keys?

Edit: My customer mapping roughly looks like this:

<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>
4

2 回答 2

0

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

您需要在映射中的客户表上指定 KEY 列。

于 2013-10-18T16:49:04.530 回答
0

我发现了问题:

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

于 2013-10-20T18:20:32.477 回答