user3639982
user3639982

Reputation: 33

Hibernate query and Session cache confusion

I am using Hibernate in a bulk DB export application. After reading of many questions/answers and articles about Session cache i still do not understand following:

assuming entity list is loaded per HQL to iterate over it and to read values of referenced objects/collections. Will be in this case referenced objects/collections saved in Session Cache?

Upvotes: 2

Views: 659

Answers (1)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 153690

Yes it does.

When you execute a HQL query all returned Entities must be loaded in the current Session. This is because the Entities are in the ATTACHED state, so any modification can be detected by the "dirty-checking mechanism".

If you are inserting a large number of entities, it's better to flush() and clear() after each batch, so that you remove the current entries from the current 1st level, since you won't need them in the next batch anyway.

If you are retrieving a large entity collection that fits your current RAM and you want the entities to be attached for the whole time you operate on a given entity, then at the end of the batch you will flush, and evict() the current processed entities. This time you don't clear() since you still want to keep the unprocessed entities attached, so that Hibernate can propagate the changes to the database.

Upvotes: 1

Related Questions