I have some problem with retrieving graph of objects with Fetch_Mode=JOIN.
My code looks like:
************************************************
Criteria hCriteriaUser = hbmSession.createCriteria(User.class);
//add Restrictions on User.name like "John"
Criteria hCriteriaHistory = hCriteriaUser.createCriteria("histories".LEFT_JOIN).setFetchMode("histories", FetchMode.JOIN);
//add Restrictions on History, status="active"
List userList = hCriteriaUser.list();
*************************************************
User mapping file looks like:
************************************************
<class name="User" table="users" lazy="false">
.............................................
<set name="histories" lazy="true" cascade="save-update,lock" inverse="true">
<key column="USERID" not-null="true"/>
<one-to-many class="History"/>
</set>
.......................................
****************************************************
Issues:
1) By default it's also executing SELECT SQLs on all "one-one" and "many-one" the History association.
For example, if there is one matching User with 4 histories, it's basically creating 4 additional SQLs unnecessarily for all the {many} one-one History associations.
This is a performance hit; I just want only JOIN to be executed. How can we prevent unnecessary SQLs
2) As we are using LEFT_JOIN in the above scenario, it's not retrieving all matching Users if there is no corresponding matching History.
For example, if there 5 users with name "John" and for 3 users with matching History,
I'm expecting userList to have all 5 users with HISTORY as NULL for the remaining 2 users.
If I use INNER_JOIN it's throwing LAZY Initialization Exception on User.getHistory() in client side code.
Our application is performance sensitive. I would appreciate any inputs on resolving this problem.
Thanks,R.
|