Reputation: 317
I have 4 Objects and I don't want to use session.save(obj) for every object. Is there a way to save all objects in a loop or at once.
For a loop i will have to increment the variables name, but I think that is not possible or a "good way" to solve the problem.
final Test a1 = new Test(6,"AA", 50, 100000,"20.04.2016","AAA", 111, "2016", "EEE");
final Test a2 = new Test(7,"AB", 1050, 200000,"20.04.2016","BBB", 333, "2016", "EEE");
final Test a3 = new Test(8,"AC", 40, 300000,"20.04.2016","CCC", 222, "2016", "UUU", "YY", 5, "SSS");
final Test a4 = new Test(9,"PD", 400, 400000,"20.04.2016","DDD", 444, "2016", "CCC", "YY", 12, "RRR");
session.beginTransaction();
session.save(a1);
session.save(a2);
session.save(a3);
session.save(a4);
session.getTransaction().commit()
Upvotes: 0
Views: 785
Reputation: 101
Please read about batch processing, it should be useful for you.
http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch15.html
For example:
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();
Upvotes: 1
Reputation: 19976
Please, use an array
Test[] tests = new Test[] {new Test(), new Test()};
for(Test test : tests) {
session.save(test);
}
Upvotes: 1