Hibernate一对多关联错误

Hibernate One to many association error

大家好,我是休眠新手。 我无法弄清楚为什么我的数据库中的条目没有更新...我的应用程序有 3 个实体:合同、银行、承包商。

每份合同只与一个承包商和银行相关联。

一个承包商或银行可以有多个合同。

Contract.java

private String contractId;
private Contract contractor
private Bank bank;

Contractor.java

private long contractorId;
private Set<Contract> contracts

Bank.java

private bankId;
private Set<Contract> contracts;

Hibernate.hbm.cfg

<class name="com.anksys.bgms.model.Contractor" table="BGMS_CONTRACTOR">
    <id name="contractorId" column="CONTRACTOR_ID" type="long">
        <generator class="identity"></generator>
    </id>
    <set name="contracts" table="BGMS_CONTRACT" inverse="true" lazy="true">
        <key>
            <column name="CONTRACT_ID" not-null="true"></column>
        </key>
        <one-to-many class="com.anksys.bgms.model.Contract"/>
    </set>
</class>
<class name="com.anksys.bgms.model.Bank" table="BGMS_BANK">
    <id name="bankId" column="BANK_ID" type="long">
        <generator class="identity"></generator>
    </id>
    <set name="contracts" table="BGMS_CONTRACT" lazy="true">
        <key>
            <column name="CONTRACT_ID" not-null="true"></column>
        </key>
        <one-to-many class="com.anksys.bgms.model.Contract"/>
    </set>
</class>

<class name="com.anksys.bgms.model.Contract" table="BGMS_CONTRACT">
    <id name="contractId" column="CONTRACT_ID" type="string"></id>
    <many-to-one name="contractor" class="com.anksys.bgms.model.Contractor"></many-to-one>
    <many-to-one name="bank" class="com.anksys.bgms.model.Bank"></many-to-one>
</class>

数据库结构:

bgms_bank: 
BANK_ID     bigint(20)  No      
Index: PRIMARY  BTREE   Yes No  CONTRACTOR_ID   4   A   No

bgms_contract:
CONTRACT_ID     varchar(50)     No      
CONTRACTOR_ID   bigint(20)  No      
BANK_ID     bigint(20)  No  
INDEX: 
Contract.contractor_id references(contractor.contractor_ID)
Contract.Bank_ID references(bank.BANK_ID)

bgms_contractor:
CONTRACTOR_ID   bigint(20)  No
INDEX: PRIMARY  BTREE   Yes No  CONTRACTOR_ID           

最后:收到: org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; SQL [insert into BGMS_CONTRACT (GUARANTEE_AMOUNT, VALID_FROM, VALID_UPTO, EXTENDED_UPTO, contractor, bank, CONTRACT_ID) values (?, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update

Caused by: java.sql.BatchUpdateException: Unknown column 'contractor' in 'field list'

试试这个:

<class name="com.anksys.bgms.model.Contract" table="BGMS_CONTRACT">
    <id name="contractId" column="CONTRACT_ID" type="string"></id>
    <many-to-one name="contractor" class="com.anksys.bgms.model.Contractor" column="contractor_id"></many-to-one>
    <many-to-one name="bank" class="com.anksys.bgms.model.Bank" column="bank_id"></many-to-one>
</class>

我已将 column 属性添加到 <many-to-one> 标签