• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Basic Hibernate question regarding Bulk Insertions

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am working on an application what uses Hibernate to populate (initialize)a DB with a large web of interconnected objects. It has several many-to-many link tables, etc.. I don't have a problem with mapping the objects, but how to cleanly handle massive number of insertions using Hibernate's API.

I have followed the advice of the Hibernate docs and have a sythetic key assigned from a sequence in the DB. Now when I insert a duplicate object Hibernate has no problem inserting a new row in the DB with a new sequence id. If I give the Business Key a unique constraint an exception is thrown. Now I don't like the idea of finding out about a duplicate through an exception.

I was wondering what is the standard way that this situation is handled. Is it to first issue a Query (or get()) on the root object and every single member of its collections on each insert to make sure they don't exist and if they do reuse that object?

I know this is probably a very newbee question, but any guidance would be greatly appreciated.

Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread covers the downside of doing a query for a duplicate before performing the insert instead of using a unique index and responding to the exception. Long story short, there's a race condition that forces you to respond to the exception anyway, so you may as well put the duplicate-handling code in one place.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!