2

Hibernate uses first level cache by default and cache the queries or i say entities for a session.

But what about projection. Lets say i have fired a join query from 3 table and get a small projection of it. How will first level cache be used here?

I mean first level cache on domain recognise the entities by Id field, but there is Id field in projection? So does hibernate first level cache does not work when uses projection?

FYI : As per my knowledge, first level cache recognise entities by Id field(primary key), please correct me if i am wrong.

Ankit Bansal
  • 2,162
  • 8
  • 42
  • 79
  • You are right, object in first level cache can be retrieved only by id. Using projections wont work. Take a look to query cache and the second level cache – Wilder Valera Mar 28 '18 at 20:46
  • You need to enable the query cache and second level cache, and then mark your projection query as cacheable. There a good blog post here with more details: https://vladmihalcea.com/hibernate-query-cache-dto-projection/ – codemonkey Mar 28 '18 at 23:32

0 Answers0