嗨,我是hibernate的新手。 我无法弄清楚为什么条目没有在我的数据库中更新...我的应用程序有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'
答案 0 :(得分:0)
试试这个:
<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>
代码