l0r*_*c10 0 java sql hibernate
我有大约500.000行数据要插入到一个表中.
我目前正在插入一个(我知道它很糟糕),如下所示:
道法:
public static final String SET_DATA = "insert into TABLE (D_ID, N_ID, VALUE, RUN_ID) " + "values (?, ?, ?, ?)";
public void setData(String dId, String nId, BigDecimal value, Run run) throws HibernateException {
if (session == null) {
session = sessionFactory.openSession();
}
SQLQuery select = session.createSQLQuery(SET_DATA);
select.setString(0, dId);
select.setString(1, nId);
select.setBigDecimal(2, value);
select.setLong(3, run.getRunId());
select.executeUpdate();
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能更有效地做到这一点?
为什么你去手写SQL查询?如果你以这种方式编写sql,你肯定无法获得休眠的成果.
学习Batch Insert示例代码Batch Insert,
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
Customer customer = new Customer(.....);
session.save(customer);
if ( i % 20 == 0 ) { //20, same as the JDBC batch size
//flush a batch of inserts and release memory:
session.flush();
session.clear();
}
}
tx.commit();
session.close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
145 次 |
| 最近记录: |