Hello All,
I am new to hibernate and will want to do batch insert.
I came across this code below but my Netbeans IDE 8.1 is unable to resolve
Quote:
org.hibernate.jdbc.Work
and session.doWork .The Hibernate framework in the IDE is 4.3
Please assist .
import org.hibernate.jdbc.
Work;
public void saveBatch ( List list) {
Transaction trns = null;
Session session = HibernateUtil.getSessionFactory().openSession();
trns = session.beginTransaction();
session.doWork(new Work() {
@Override
public void execute(Connection conn) throws SQLException {
PreparedStatement pstmt = null;
try{
String sqlInsert = "insert into sampletbl (name) values (?) ";
pstmt = conn.prepareStatement(sqlInsert );
int i=0;
for(String name : list){
pstmt .setString(1, name);
pstmt .addBatch();
//20 : JDBC batch size
if ( i % 20 == 0 ) {
pstmt .executeBatch();
}
i++;
}
pstmt .executeBatch();
}
finally{
pstmt .close();
}
}
});
trns.commit();
session.close();
}