冬眠nativesqlquery批量插入 [英] hibernate nativesqlquery batch insert

查看:1094
本文介绍了冬眠nativesqlquery批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的原因,没有必要透露,我需要运行一系列使用休眠层本地的SQL语句。他们是形式的插入ABC(列1,列2)值(:column1List,:column2List)相同的语句(相同的绑定变量)。

For reasons, not needed to divulge, I need to run a series of native SQL statements using the hibernate layer. They are the same statement (same bind variables) of the form "insert abc(column1,column2) values(:column1List, :column2List)".

我想执行这个作为批量插入如果可能的话。能不能做到?如果是的话怎么样?

I would like to execute this as a batch insert if possible. Can it be done ? If so how ?

我试过

的SQLQuery = session.createSQLQuery(sqlquery的);
sqlQuery.setParameterList(.....)

我想我找到了原因,但我不知道发生了什么。 INSERT语句具有超过2列,Hibernate是改变它变成INSERT INTO ABC(列1,列2,栏3,column4,column5,column6)VALUES(?,?,(?,?),(?,?),(? ,?)?)

I think I found out the reason but I am not sure what's happening. The insert statement has more than 2 columns and hibernate is changing it into insert into abc(column1, column2, column3, column4,column5, column6) values ( ?,?, (?,?),(?,?),(?,?),?)

推荐答案

我在休眠4.1.4。我尽了浑身解数我能想到的,使其工作。因此,我在休眠4.我不得不求助于实施一类没有工作org.hibernate.jdbc.Work。有一次我一个语句句柄,一切都顺利,我得到了我的答案。这种方法是正确的通过DaveB概括。

I am on hibernate 4.1.4. I tried every trick I can think of to make it work. Hence I am on hibernate 4. I had to resort to implementing a class that did work "org.hibernate.jdbc.Work". Once I have a statement handle, everything went smooth and I got my answer. The approach is correct outlined by DaveB.

Transaction tx = session.beginTransaction() ;
  SqlWork sqlWork = new SQLWork(a,b,c) ; // used inside execute <br/>
  session.doWork(sqlWork) ;
  tx.commit();
} catch (HibernateException he) {
tx.rollback();
} finally {
session.close()
}

这篇关于冬眠nativesqlquery批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆