Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate cache level 1

I have one misunderstanding of cache level 1. So far I've seen that cache level 1 is used only to put off sql invocations until a transaction is flushed.

But what about objects? For instance, I use only cache level 1. If I were to load the same object several times within 1 transaction, what would happen? Does hibernate use some sort of identity map and caches objects which has been loaded within 1 transaction?

like image 855
Aleksey Avatar asked Jan 25 '26 07:01

Aleksey


1 Answers

So far I've seen that cache level 1 is used only to put off sql invocations until a transaction is flushed.

To put it simply, the first level cache is just a map from id (primary key) to an object that holds the state associated with that primary key.

But you shouldn't mix session and transaction concepts, they are not the same thing (and a session is not necessarily transaction scoped).

If I were to load the same object several times within 1 transaction, what would happen?

There will be only one object representing a given row in the database (i.e. for a given id). That's what makes managing state possible and that's the whole point of the 1st level cache.

Does hibernate use some sort of identity map and caches objects which has been loaded within 1 transaction

See above.

like image 53
Pascal Thivent Avatar answered Jan 27 '26 01:01

Pascal Thivent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!