Reputation: 1237
Why does hibernate do a select before saving an object?
I can't find useful information on internet. Is this normal behavior before every save? I found this topic, select Query Run for hibernateTemplate.save() - save, but I do not find this answer "definitive". I mean, do we have to use versioning if i want to avoid this select before saving each object?
I would appreciate all the explanations or links.
Upvotes: 10
Views: 13776
Reputation: 581
So Julia is right, calling Session.save()
with an entity that has its IDs assigned results in hibernate doing a SELECT
and then an INSERT
. Fortunately there are two workarounds:
Session.persist()
rather than Session.save()
The second option also works seamlessly with Envers.
Hope this saves someone else hours of hunting.
Upvotes: 5
Reputation: 35075
I know you have answered your own question in the question's comment, but just to summarise this here are some general points.
Just to clarify, NHibernate uses 'save' as in 'SQL INSERT' and 'update' as in 'SQL UPDATE'.
I know of these common scenarios when NHibernate will fetch an object implicitly (no explicit use of s.Update) from the db before persisting it:
As with your example, this may not be obvious when parent-child objects are used (but simple rules stay the same) as it may not be obvious from the code that children will be fetched.
Upvotes: 2
Reputation: 31903
No, it doesn't do a select before a save. Are you sure your edit-save usecase is right? A common flow for webapps is:
A single sql update will be executed which also handles versioning. If the version number is out of sync, a StaleObjectStateException
will be thrown.
Upvotes: 0