Archive for December, 2016

EntityManager Batch insert

@Service
public class BatchRepository<T extends MyEntity> {

@PersistenceContext
private EntityManager em;

public List<T> saveList(List<T> list) {

Session session = (Session) em.getDelegate();
Transaction tx = session.beginTransaction();

for (int i = 0; i < list.size(); i++) {
T t = list.get(i);
if (t.getId() == null) {
session.save(t);
} else {
session.update(t);
}
}

tx.commit();
return list;
}

Advertisements

Leave a comment