Hibernate Insert / Update Constraints

I am writing a program to start a batch process on hundreds of thousands of objects of several related types. I initially did this with one transaction at one point. This seemed very slow, so I tried to do some naive batch updates as described in http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html , with longer transactions and random discards + cleanup. I use ConstraintViolationException for some types of objects because I have unique field restrictions. However, I'm not sure how to check for existing instances; I currently have criteria for listing collisions, but it does not seem to return the entities that I saved the Operation in the same transaction.

The above example may help:
Faces Family, Person, Name
There are many people in the family (from one to many)
People have many names, different people can have the same name. (Many for many)

My updates include saving the family along with its faces and names, but I'm not sure how to deduplicate the names (may come across an existing name in db or another name in the same batch of updates). I could just keep track of the unique constraint fields of new entities outside of hibernation, but I thought it probably wasn't necessary. Is there a built-in way to check for duplicates in both db and uncommitted changes? I have seen Hibernate batch updates with a violation violation exception exception , but I do not use exceptions in the normal code. Thanks, I appreciate any guidance.

+4
source share
1 answer

The short answer is no. For batch operations, Hibernate does not track the generated identifiers, so you will need to go to the database for each Name , since you will make a request based on the name, not on the identifier, if you use some query cache (which would be difficult for your business, I suppose).

I would suggest doing this in a two-step (three?) Process: first, insert all Name objects. Then load them all using Hibernate itself, saving them to a Map . Then just save the other data by binding the Name to an uninstalled Person . Of course, you will need as much memory as you have names :-) But why do you save Name as a separate object?

+2
source

All Articles