I have trouble understanding when hibernate hits the second level cache and when does it invalidate the cache.
This is what I currently understand:
Late to the party but wanted to systematically answer these question which many developers ask.
Taking your question one by one here is my answer.
Q. When does hibernate hit this cache?
A. First Level cache is associated with the Session object. The Second Level Cache is associated with the Session Factory object. If object is not found in the first, then the second level is checked.
Q. Let's say I've set up the second level cache but not the query caching. I want to cache my customers, there's 50000 of them. In what ways can I retrieve the customers from the cache?
A. You got that answered in your update. Also the query cache stores just the list of IDs of the object and those Objects w.r.t their IDs are stored in the same second level cache. So if you enable query cache, you'll utilize the same resource. Neat right ?
Q. I assume I can get them by id from cache. That would be easy but also not worthy of caching. But what if I want to do some calculation with all my customers. Let's say I want to show a list of the customers then how would I access them?
A. Answered above.
Q. How would I get all my customers if query caching is disabled?
A. Answered above.
Q. What would happen if someone updated one of the customers? Would that customer get invalidated in the cache or would all customers get invalidated?
A. Hibernate has no idea but you could use other third party IMDG / distributed caches to be implemented as hibernate second level cache and get them invalidated. e.g. TayzGrid is one such product and there are more i guess.