Hibernate version: 3.0
Mapping documents:
<hibernate-mapping package="com.onebeacon.diarytracking.ormapping.domain">
<class name="UserRoles" table="UserRoles">
<composite-id name="comp_id" class="UserRolesPK">
<key-property name="Role_CDE" column="Role_CDE" type="java.lang.String" length="25" />
<key-property name="Novell_ID" column="Novell_ID" type="java.lang.String" length="8" />
</composite-id>
<property name="Role_CDE" column="Role_CDE" type="java.lang.String" length="25" lazy="false" insert="false" update="false" />
<property name="PrimaryRole_IND" column="PrimaryRole_IND" type="java.lang.Byte" length="1" lazy="true" />
<property name="Novell_ID" column="Novell_ID" type="java.lang.String" length="8" lazy="false" insert="false" update="false" />
</class>
<sql-query name="UserRoles.getUserRoles">
<return class="UserRoles" />
<![CDATA[SELECT Novell_ID, Role_CDE, PrimaryRole_IND FROM UserRoles WHERE Novell_ID =:Novell_ID]]>
</sql-query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
//client code -
UserRolesDao _userRolesDao = UserRolesHibernateDaoFactory.create();
Map map = new HashMap();
map.put("Novell_ID", users.getNovell_ID());
List list = null;
try {
list = _userRolesDao.getUserRoles(map);
} catch (UserRolesDaoException e) {
return false;
}
//DAO Implementation
public List getUserRoles(Map map) throws UserRolesDaoException {
return hibernateProcessor.select("UserRoles.getUserRoles", map);
}
//HibernateProcessor::select()
public List select(String id, Map data)
{
Session session = HibernateUtil.currentSession(sessionFactory);
session.setFlushMode(FlushMode.NEVER);
//Transaction tx = session.beginTransaction();
List results = null;
try
{
Query qry = session.getNamedQuery(id);
String params[] = qry.getNamedParameters();
if(params!=null)
{
for(int i=0; i<params.length; i++)
{
Object paramVal = data.get(params[i]);
qry.setParameter(params[i], paramVal);
}
}
results = qry.list();
}catch(Exception e)
{
if(e instanceof JDBCConnectionException)
{
boolean flag = handleJDBCConnectionException((JDBCConnectionException) e, session, data);
if(flag)
return null;
select(id, data);
}
else
logger.error(ExceptionUtils.getFullStackTrace(e));
}finally
{
session.setFlushMode(FlushMode.COMMIT);
try
{
//if(tx!=null)
//if(tx.isActive())
//tx.rollback();
}catch(Exception e)
{}
}
return results;
}
Full stack trace of any exception that occurs:
N/A
Name and version of the database you are using:
MS SQL Server 2000
Using ANT script, UserRoles class is modified for runtime bytecode interception.
Now my question is - when I execute "UserRoles.getUserRoles" sql-query of this mapping, does it populate PrimaryRole_IND inside UserRoles class, even though PrimaryRole_IND is marked as lazy=true ?
After running multiple tests, I came to a conclusion that lazy properties are ignored when mapping sql-query resultset to POJO class. And as a result, when a getter is invoked for lazy property, it again executes another sql query to get the value for that lazy property. Is my understanding correct? If yes, how do we force Hibernate to map the resultset to POJO including lazy properties(as long as they are part of the sql projection)
Please help !!
|