在使用 MySQL db 时在 Hibernate 中调用插入存储过程时收到以下错误:
Hibernate:
{ call InsertPayment(?, ?) }
sie 28, 2013 10:17:19 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: S1009
sie 28, 2013 10:17:19 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Parameter index out of range (3 > number of parameters, which is 2).
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [model_mapping_xml.TPayment]
MySQL db中的存储过程定义:
CREATE PROCEDURE InsertPayment(
IN pIdAnother INT,
IN pAmount DECIMAL(19,4)
)
BEGIN
...
END
TPayment.hbm.xml 文件包含:
<sql-insert callable="true" check="none">
{ call InsertPayment(?, ?) }
</sql-insert>
存储过程的隐式调用:
// calling the stored procedure to add payment
TPayment newp = new TPayment();
newp.setAnother((TAnother) session.load(TAnother.class, 1));
newp.setAmount(BigDecimal.valueOf(20));
session.save(newp);
为什么它说“3 > 参数数量”到处都有这个过程的 2 个参数?(我可以以类似的方式调用 deletePayment 和 modifyPayment 存储过程,它们工作正常......)。
付款映射:
<hibernate-mapping>
<class name="model_mapping_xml.TPayment" table="TPayment" catalog="DB">
<id name="idPayment" type="java.lang.Integer">
<column name="IdPayment" />
<generator class="identity" />
</id>
<version name="rowvers" type="timestamp" generated="always">
<column name="Rowvers" length="19" not-null="true" />
</version>
<many-to-one name="another" class="model_mapping_xml.TAnother" fetch="select">
<column name="IdAnother" not-null="true" />
</many-to-one>
<property name="amount" type="big_decimal">
<column name="Amount" scale="4" not-null="true" />
</property>
<property name="date1" type="timestamp">
<column name="Date1" length="19" not-null="false" />
</property>
<sql-insert callable="true" check="none">
{ call InsertPayment(?, ?) }
</sql-insert>
<sql-update callable="true" check="none">
{ call ModifyPayment(?, ?, ?, ?, ?) }
</sql-update>
<sql-delete callable="true" check="none">
{ call DeletePayment(?, ?) }
</sql-delete>
</class>
</hibernate-mapping>
T另一个映射:
<hibernate-mapping>
<class name="model_mapping_xml.TAnother" table="TAnother" catalog="DB">
<id name="idAnother" type="java.lang.Integer">
<column name="IdAnother" />
<generator class="identity" />
</id>
<property name="dateBegin" type="date">
<column name="DateBegin" length="10" not-null="true" />
</property>
<property name="dateEnd" type="date">
<column name="DateEnd" length="10" />
</property>
<property name="rowvers" type="timestamp">
<column name="Rowvers" length="19" not-null="true" />
</property>
<set name="payment" table="TPayment"
inverse="true" lazy="true" fetch="select">
<key>
<column name="IdAnother" not-null="true" />
</key>
<one-to-many class="model_mapping_xml.TPayment" />
</set>
</class>
</hibernate-mapping>