Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.0.2
Mapping documents:
Code between sessionFactory.openSession() and session.close():
public void insertOrgUnits() {
ArrayList list = new ArrayList();
try {
for (int i = 0; i < 10000; i++) {
OrganisationUnitBean bean = new OrganisationUnitBean();
bean.setDescription("hej " + i);
bean.setName("name " + i);
bean.setNumber(new Integer(i));
OrganisationTypeBean organisationTypeBean = new OrganisationTypeBean();
organisationTypeBean.setId(new Long(1));
bean.setOrganisationType(organisationTypeBean);
bean.setShortName("shortname " + i);
list.add(bean);
}
insertOrgUnits2(list);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void insertOrgUnits2(ArrayList list) {
Session session = getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Iterator iterator = list.iterator();
int i = 0;
while (iterator.hasNext()) {
i++;
OrganisationUnitBean bean = (OrganisationUnitBean) iterator.next();
Long id = (Long) session.save(bean);
if (i % 20 == 0) {
System.out.println("flush and clear");
session.flush();
session.clear();
}
System.out.println(" i : " + i + " saved : " + id);
}
tx.commit();
} catch (Exception e) {
System.out.println(e.getMessage());
try {
tx.rollback();
} catch (HibernateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} finally {
try {
session.close();
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
}
I am trying to do a bacth insert like the documentation says, but the output when I execute the code is :
i : 1 saved : 74484
i : 2 saved : 74485
i : 3 saved : 74486
i : 4 saved : 74487
i : 5 saved : 74488
i : 6 saved : 74489
i : 7 saved : 74490
i : 8 saved : 74491
i : 9 saved : 74492
i : 10 saved : 74493
i : 11 saved : 74494
i : 12 saved : 74495
i : 13 saved : 74496
i : 14 saved : 74497
i : 15 saved : 74498
i : 16 saved : 74499
i : 17 saved : 74500
i : 18 saved : 74501
i : 19 saved : 74502
flush and clear
after this nothing happens.
What I am doing wrong here.