當(dāng)想通過hibernate進(jìn)行批量增加記錄的時(shí)候,或者到數(shù)據(jù)的時(shí)候會(huì)用到SQL語(yǔ)句。處理如下:
public Integer batchSave(final String sql){
Session session = this.getSession();
Transaction tx = session.beginTransaction();
Integer result = -1;
try {
tx.begin();
result = session.createSQLQuery(sql).executeUpdate();
session.flush();
tx.commit();
} catch (DataAccessException e) {
e.printStackTrace();
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return result;
}