1

I have been using Hibernate/Spring. I have a small doubt. If we get 1000 records from one table and save to another table using saveOrUpdate() method it saves.

If I call the same method by passing same 1000 records without modifying any record, how hibernate behaves here? Does it fires 1000 update queries or it cleverly identifies whether record is really modified or not?

Thanks!

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
user1016403
  • 12,151
  • 35
  • 108
  • 137

3 Answers3

2

I don't think it will execute an update regardless the state. An entity in Hibernate has some kind of dirty flag that indicates whether the object has been altered since it was retreived from the database.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

If those 1000 records were loaded and persisted using same session object it won't fire any update scripts. If 1000 records were loaded and saved with different session objects then hibernate will fire 1000 update queries.

Pokuri
  • 3,072
  • 8
  • 31
  • 55
0

I don't think it will do updates if you use a different session here.

In order to perform the saveOrUpdate Hibernate will first load the entity in to the new session. It will then check to see if the entity you're saving is dirty. If it's not dirty then no update will be performed.

The key here is that Hibernate will not operate on the object unless it's in the session. Whether you're using the same session or a new one.

Alex Barnes
  • 7,174
  • 1
  • 30
  • 50