Guest User

Untitled

a guest
Jan 4th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. public void saveOrUpdate(Object dataHierarchyRoot) {
  2. final EntityManager entityManager = entityManagerFactory.createEntityManager();
  3. final EntityTransaction transaction = entityManager.getTransaction();
  4.  
  5. try {
  6. transaction.begin();
  7.  
  8. // This single call may result in inserting up to 10K records
  9. entityManager.merge(dataHierarchyRoot);
  10. transaction.commit();
  11. } catch (final Throwable e) {
  12. // error handling redacted for brevity
  13. } finally {
  14. entityManager.close();
  15. }
  16. }
  17.  
  18. for (Object entity: entities) {
  19. if(entity.getId() == null) {
  20. entityManager.persist(entity);
  21. } else {
  22. entityManager.merge(entity);
  23. }
  24. if ((i % batchSize) == 0) {
  25. entityManager.getTransaction().commit();
  26. entityManager.clear();
  27. entityManager.getTransaction().begin();
  28. }
  29. }
  30. entityManager.getTransaction().commit();
  31. em.getTransaction().commit();
  32.  
  33. <persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
  34. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  35. <properties>
  36. <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
  37. <property name="hibernate.connection.username" value="***"/>
  38. <property name="hibernate.connection.password" value="***"/>
  39. <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
  40. <property name="hibernate.connection.url" value="jdbc:oracle:thin:@***"/>
  41. <property name="hibernate.jdbc.batch_size" value="100"/>
  42. </properties>
  43. </persistence-unit>
  44.  
  45. Session session = SessionFactory.openSession();
  46. Transaction tx = session.beginTransaction();
  47. for ( int i=0; i<100000; i++ ) {
  48. Employee employee = new Employee(.....);
  49. session.save(employee);
  50. }
  51. tx.commit();
  52. session.close();
Add Comment
Please, Sign In to add comment