Does Hibernate put collection that was retrived from data base to first lvl cache?

Viewed 38

Can someone explain me is it true that hibernate put collection to first lvl cache

is it looks like

frist request by RestController

@GetMapping("/findfoo")
public List<Foo> findFoo(){
List<Foo> listFirst=fooRepository.findAll();
return listFirst;
}

hibernate will do this : select * from foo; and second requet

List<foo> listTwo=fooRepositoy.findAll();

hibernate will get listFirst from first lvl cache ?or it will do the same select to data base?

1 Answers

The entities are put in the 1st level cache (the session) but not the result of queries, for that you would need to enable the 2nd level query cache.

To put it differently, the Hibernate session keeps track of all the entities whether they are loaded from a query (as it is the case here) or by lazy loading an association from another entity.

If you do the query twice, it will the database with the 2nd query too, however Hibernate will return the same java objects (== identity, not equals() identity).

Related