Hibernate, LazyLoad and perfomance optimization.


General J2EE: Hibernate, LazyLoad and perfomance optimization.

  1. I faced problem with optimization of Hibernate fetching. For instance, I have a class User with included Set of permissions (many-to-many). Sometimes I need user with them, sometimes w/o. If I used Lazy loading I'd get my permissions loaded w/ extra SQL query. But I don't want it. I need to load all at one time i.e. using outer-join. I've got my solution for now. The solution is to have two different classes for uset w/ permissions and w/o them. Both these classes are mapped to one table. If I need full user I will use FullUser class. If I don't I'll use User class. By using this solution I'll get SQL optimization but also get more comlicated code. My DAO will have several similar methods for loading user like: loadFullUser() and loadUser(). Also these classes would implement one interface to unify their usage. But it all doesn't look good to me. Any suggestion is appreciated
  2. Use a single class mapped to the table, skip the idea of multiple classes since it will give you an major headake later on for sure! Instead, pass a parameter into your dao's fetch method toggling if you want a lazy fetch or not. Depending on the flag, you write a HQL statement for fetching the user using the "fetch" keyword (see the Hibernate docs) in the outer join with the permissions in the case you need a full fetch, and otherwise you omit the keyword (rendering a lazy fetch) Regards Niklas
  3. Thanks! It's of use for me.