meenakshi sundar wrote:is there a way to tell hibernate engine to cache the sizes of the lazy collection so it doesn't need to hit the database when we do users.size() ?
If you want a standard JPA solution, you'll probably need to create a NamedQueryIf you don't mind using a specific Hibernate solution, you can use this annotation @LazyCollection(LazyCollectionOption.EXTRA). Using this annotation you get "smart" collection behavior, i.e. some collection operations such as size(), contains(), get(), etc. do not trigger collection initialization.
You'll find more information here and here. And beware there seems to be a bug when using LazyCollectionOption.EXTRA in combination with @Where.