Turned DEBUG logging on these classes :
Code:
log4j.logger.net.sf.ehcache=DEBUG, HibernateAppender
log4j.logger.org.hibernate.cache=DEBUG, HibernateAppender
log4j.logger.org.hibernate.proxy=DEBUG, HibernateAppender
log4j.logger.org.hibernate.loader=DEBUG, HibernateAppender
log4j.logger.org.hibernate.intercept=DEBUG, HibernateAppender
log4j.logger.org.hibernate.criterion=DEBUG, HibernateAppender
Noticed by looking at the logs that Hibernate is actually creating the child entities correctly when querying the table on the first time.
But it ignores that fact and queries the database again when you try to read them using the size() method.
Here are the logs I have from this issue :
This is the first criteria code :Code:
// Force the load of all entities by using FetchMode.JOIN
Criteria criteria = hbSession.createCriteria(UserHB.class, "user").
createAlias("user.userRoles", "roles").
createAlias("roles.businessRole", "buRole").
createAlias("roles.businessEntityType", "buEntityType").
add(Restrictions.eq("buEntityType.comp_id.businessEntityTypeId", new Short(businessEntityTypeId))).
add(Restrictions.eq("roles.comp_id.businessEntityId", businessEntityId)).
add(Restrictions.eq("user.comp_id.rptgQtr", reportingQuarter)).
add(Restrictions.eq("user.comp_id.userId", "632744897")).
setFetchMode("roles", FetchMode.JOIN).
setFetchMode("buRole", FetchMode.JOIN).
setFetchMode("buEntityType", FetchMode.JOIN).
setCacheable(false);
Short[] ids = new Short[((List)businessRoleId).size()];
int count = 0;
for (Iterator i = ((List) businessRoleId).iterator(); i.hasNext(); count++) {
ids[count] = new Short((String)i.next());
}
result = criteria.add(Restrictions.in("buRole.comp_id.businessRoleId", ids)).list();
Result SQL generated from the criteria above :Code:
Hibernate: select this_.RPTG_QTR as RPTG1_165_3_, this_.USER_ID as USER2_165_3_, this_.EMAIL as EMAIL165_3_, this_.FIRST_NAME as FIRST4_165_3_, this_.LAST_NAME as LAST5_165_3_, this_.DIVISION as DIVISION165_3_, this_.DEPARTMENT as DEPARTMENT165_3_, this_.INVALID_DATE as INVALID8_165_3_, roles1_.RPTG_QTR as RPTG1_162_0_, roles1_.USER_ID as USER2_162_0_, roles1_.BUSINESS_ROLE_ID as BUSINESS3_162_0_, roles1_.BUSINESS_ENTITY_ID as BUSINESS4_162_0_, roles1_.RPTG_QTR_BUS_ENT_TYPE as RPTG6_162_0_, roles1_.RPTG_QTR_BUS_ROLE as RPTG7_162_0_, roles1_.RPTG_QTR_USER as RPTG8_162_0_, roles1_.BUSINESS_ENTITY_TYPE as BUSINESS5_162_0_, burole2_.RPTG_QTR as RPTG1_16_1_, burole2_.BUSINESS_ROLE_ID as BUSINESS2_16_1_, burole2_.BUSINESS_ROLE_NAME as BUSINESS3_16_1_, burole2_.RETIRED as RETIRED16_1_, buentityty3_.RPTG_QTR as RPTG1_15_2_, buentityty3_.BUSINESS_ENTITY_TYPE_ID as BUSINESS2_15_2_, buentityty3_.BUSINESS_ENTITY_TYPE_KEY as BUSINESS3_15_2_, buentityty3_.BUSINESS_ENTITY_TYPE_NAME as BUSINESS4_15_2_, buentityty3_.RETIRED as RETIRED15_2_ from USER this_ inner join USER_ROLE roles1_ on this_.RPTG_QTR=roles1_.RPTG_QTR and this_.USER_ID=roles1_.USER_ID inner join BUSINESS_ROLE burole2_ on roles1_.RPTG_QTR=burole2_.RPTG_QTR and roles1_.BUSINESS_ROLE_ID=burole2_.BUSINESS_ROLE_ID inner join BUSINESS_ENTITY_TYPE buentityty3_ on roles1_.RPTG_QTR=buentityty3_.RPTG_QTR and roles1_.BUSINESS_ENTITY_TYPE=buentityty3_.BUSINESS_ENTITY_TYPE_ID where buentityty3_.BUSINESS_ENTITY_TYPE_ID=? and roles1_.BUSINESS_ENTITY_ID=? and this_.RPTG_QTR=? and this_.USER_ID=? and burole2_.BUSINESS_ROLE_ID in (?, ?, ?)
This is the logging of this query's execution :Code:
[DEBUG] org.hibernate.loader.Loader getRow {result row: EntityKey[hibernate.UserRoleHB#component[rptgQtr,userId,businessRoleId,businessEntityId]{userId=632744897, rptgQtr=2007 Q2, businessRoleId=1, businessEntityId=GPC300000162}], EntityKey[hibernate.BusinessRoleHB#component[rptgQtr,businessRoleId]{rptgQtr=2007 Q2, businessRoleId=1}], EntityKey[hibernate.BusinessEntityTypeHB#component[rptgQtr,businessEntityTypeId]{businessEntityTypeId=1, rptgQtr=2007 Q2}], EntityKey[hibernate.UserHB#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader getRow {result row: EntityKey[hibernate.UserRoleHB#component[rptgQtr,userId,businessRoleId,businessEntityId]{userId=632744897, rptgQtr=2007 Q2, businessRoleId=1, businessEntityId=GPC300000162}], EntityKey[hibernate.BusinessRoleHB#component[rptgQtr,businessRoleId]{rptgQtr=2007 Q2, businessRoleId=1}], EntityKey[hibernate.BusinessEntityTypeHB#component[rptgQtr,businessEntityTypeId]{businessEntityTypeId=1, rptgQtr=2007 Q2}], EntityKey[hibernate.UserHB#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader loadEntity {done entity load}
[DEBUG] org.hibernate.loader.Loader loadEntity {done entity load}
The first line on the log output reveals that Hibernate loaded the UserRoleHB entity :Code:
[DEBUG] org.hibernate.loader.Loader getRow {result row: EntityKey[hibernate.UserRoleHB#component[rptgQtr,userId,businessRoleId,businessEntityId]{userId=632744897, rptgQtr=2007 Q2, businessRoleId=1, businessEntityId=GPC300000162}]
...
...
[DEBUG] org.hibernate.loader.Loader loadEntity {done entity load}
Okay, here comes the problem in a more detailed explanation.
This is the code for testing if the entities are cached/loaded correctly :Code:
UserHB user = null;
for (Iterator i = result.iterator(); i.hasNext(); ) {
user = (UserHB)i.next();
user.getUserRoles().size();
}
This is the log generated when the user.getUserRoles().size(); line is executed :Code:
[DEBUG] org.hibernate.loader.Loader loadCollection {loading collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader loadCollection {loading collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
Hibernate: select userroles0_.RPTG_QTR as RPTG1_1_, userroles0_.USER_ID as USER2_1_, userroles0_.BUSINESS_ROLE_ID as BUSINESS3_1_, userroles0_.BUSINESS_ENTITY_ID as BUSINESS4_1_, userroles0_.RPTG_QTR as RPTG1_162_0_, userroles0_.USER_ID as USER2_162_0_, userroles0_.BUSINESS_ROLE_ID as BUSINESS3_162_0_, userroles0_.BUSINESS_ENTITY_ID as BUSINESS4_162_0_, userroles0_.RPTG_QTR_BUS_ENT_TYPE as RPTG6_162_0_, userroles0_.RPTG_QTR_BUS_ROLE as RPTG7_162_0_, userroles0_.RPTG_QTR_USER as RPTG8_162_0_, userroles0_.BUSINESS_ENTITY_TYPE as BUSINESS5_162_0_ from USER_ROLE userroles0_ where userroles0_.RPTG_QTR=? and userroles0_.USER_ID=?
[DEBUG] org.hibernate.loader.Loader handleEmptyCollections {result set contains (possibly empty) collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader handleEmptyCollections {result set contains (possibly empty) collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader getRow {result row: EntityKey[hibernate.UserRoleHB#component[rptgQtr,userId,businessRoleId,businessEntityId]{userId=632744897, rptgQtr=2007 Q2, businessRoleId=1, businessEntityId=GPC300000162}]}
[DEBUG] org.hibernate.loader.Loader getRow {result row: EntityKey[hibernate.UserRoleHB#component[rptgQtr,userId,businessRoleId,businessEntityId]{userId=632744897, rptgQtr=2007 Q2, businessRoleId=1, businessEntityId=GPC300000162}]}
[DEBUG] org.hibernate.loader.Loader readCollectionElement {found row of collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader readCollectionElement {found row of collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader getRow {result row: EntityKey[hibernate.UserRoleHB#component[rptgQtr,userId,businessRoleId,businessEntityId]{userId=632744897, rptgQtr=2007 Q2, businessRoleId=1, businessEntityId=GPC300000161}]}
[DEBUG] org.hibernate.loader.Loader getRow {result row: EntityKey[hibernate.UserRoleHB#component[rptgQtr,userId,businessRoleId,businessEntityId]{userId=632744897, rptgQtr=2007 Q2, businessRoleId=1, businessEntityId=GPC300000161}]}
[DEBUG] org.hibernate.loader.Loader readCollectionElement {found row of collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader readCollectionElement {found row of collection: [hibernate.UserHB.userRoles#component[rptgQtr,userId]{userId=632744897, rptgQtr=2007 Q2}]}
[DEBUG] org.hibernate.loader.Loader loadCollection {done loading collection}
[DEBUG] org.hibernate.loader.Loader loadCollection {done loading collection}
So, why is it querying the DB again for the same information it saved on the first criteria ?
From the logs I don't see any calls to caching or related methods, shouldn't we see some caching method calls on the second piece of logs ?
Should I add logging to any other hibernate package to get more info ?
Hope this info help us to solve this problem.
Thank you.