我想使用hibernate执行mysql插入查询,我有这个:
String q1="INSERT INTO sample (DatasetStatusID,Message,ProcessTime) VALUES('?','?','?') ";
Query query=session.createSQLQuery(q1);
query.setParameter(0, 5);
query.setParameter(1, "TESTING");
query.setParameter(2, new Date());
int result=query.executeUpdate();
tx.commit();
我发生了以下异常,
Exception in thread "main" org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1
这里出了什么问题?
最佳答案
如果您使用HIBERNATE
,推荐的保存数据方法是save()
。 HIBERNATE
拥有数据库操作的所有方法。但有些原因您可以使用native sql
查询。使用 hibernate
保存如下所示:
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
sess.save(YourPojo);
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}
推荐的 Hibernate 教程:
Hibernate Reference Documentation
Hibernate Tutorial
关于mysql - Hibernate 异常位置超出声明的序数参数的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30116036/