• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bulk Updates and Inserts using JPA

 
Ranch Hand
Posts: 462
Scala jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm very new to JPA and have inherited some code which looks pretty inefficient. It's job is to cancel some cashflows in the database by updating the original cashflow's isLatestVersion flag and endDate and inserting new versions with a status of cancelled. To do this it gets all the cashflows to update and then loops through each one, cloning it, updating fields, and then calling EntityManager.persist() for the new cashflow and EntityManager.merge() for the updated cashflow. The code looks like:



Is there a way in JPA to do the inserts in one go without having to loop through each one?

Any other ways to make this more efficient? I don't think there's any transaction flags set anywhere so would doing the work inside a transaction help speed it up (I'm assuming the default is AutoCommit or something similar)?

Thanks
Willy
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot persist anything in JPA without a transaction, so I assume you have one somewhere.
Normally in large batch situations splitting the batch into smaller batches per transaction (say 1000) is more efficient, but this depends on how big the batch is.

Most JPA providers support batch writing, which depending on the database can improve performance dramatically.

You seem to be recording history, instead of using persist, you could just create an update trigger in your database that does this. If you are using EclipseLink, it provides history support that automates the historization and allows historical querying.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should optimize Hibernate for bulk insert. You can check this link where I explain different bulk insertion optimization techniques while also measuring performance gains on examples:

http://korhner.github.io/hibernate/hibernate-performance-traps-part-2/

Good luck!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic