Hi All,
I am using Hibernate Version 2.1 with SAPDB 7.4.
The following is the Java code in question
Code:
public class User
{
private Long userId = null;
...
...
...
private User createBy = null;
private User updateBy = null;
public Long getUserId()
{
return this.userId();
}
public void setUserId(Long userId)
{
this.userId = userId
}
...
...
...
public User getCreateBy()
{
return this.createBy;
}
public void setCreateBy(User createBy)
{
this.createBy = createBy;
}
public User getUpdateBy()
{
return this.updateBy;
}
public void setUpdateBy(User updateBy)
{
this.updateBy = updateBy;
}
}
The following is the corresponding mapping from the XML Document.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="User" table="USERS" dynamic-update="false" dynamic-insert="false">
<id name="userId" column="USERID" type="java.lang.Long">
<generator class="sequence">
<param name="userId">SEQ_USERID</param>
</generator>
</id>
<many-to-one name="createBy" class="User" cascade="none"
outer-join="false" update="false" insert="true"
column="CREATEBY" not-null="true" unique="false"/>
<many-to-one name="updateBy" class="User" cascade="none"
outer-join="false" update="true" insert="true"
column="UPDATEBY" not-null="true" unique="false"/>
</class>
</hibernate-mapping>
I am able to retrieve the object successfully in my code doing the following.
Code:
net.sf.hibernate.Session hibernateSession = HibernateManager.currentSession();
Transaction tx = hibernateSession.beginTransaction();
Criteria userCriteria = hibernateSession.createCriteria(User.class);
userCriteria.add(Expression.eq("username", "gpani"));
userCriteria.setMaxResults(1);
ArrayList userList = (ArrayList) userCriteria.list();
tx.commit();
hibernateSession.close();
if (userList != null)
{
user = (User) userList.get(0);
logger.debug("UserId: " + user.getUserId());
logger.debug("Created By: " + user.getCreateBy().getUserId());
logger.debug("Updated By: " + user.getUpdateBy().getUserId());
}
HibernateManager.closeSession();
The thing that concerns me is that due to the nested nature of the User object in the form of createBy and updateBy within the User itself, I can do the following and get values.
Code:
logger.debug("Updated By - Update By: " + user.getUpdateBy().getUpdateBy().getUserId());
logger.debug("Updated By - Update By - Update By: " + user.getUpdateBy().getUpdateBy().getUpdateBy().getUserId());
...
...
...
While I have the createBy and updateBy objects in all my other objects to maintain an audit trail, the only one where it becomes an issue is the User object itself. I have considered the option of having the createBy and updateBy as the Long userId value for the User object only. But if there is any way to use my existing model I would like to stick to it.
When I execute the code it doesn't crash the system like you would expect it to should it go into an infinite loop. But at the same time, it seems like it is in an infinite loop. Has anyone else faced this sort of a problem and what kind of solution did you adopt?
Any help is appreciated.
TIA