Hibernate version:
hibernate-2.1.3
Mapping documents:
<hibernate-mapping>
<class name="com.jff.bo.UserBean" table="users" lazy="true">
<cache usage="read-write"/>
<id name="user_id" type="int">
<generator class="native"/>
</id>
<many-to-one name="role" class="com.jff.bo.RoleBean" column="role_id_fk"/>
<property name="login" column="login" type="string" update="false" not-null="true" unique="true"/>
<property name="password" column="password" type="string" not-null="true"/>
<property name="registration_date" column="registration_date" type="timestamp" update="false" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
First i call to:
Code:
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
UserBean sessionUser = sessionMgr.getSessionUser(httpServletRequest.getSession(true));
if (sessionUser != null) {
userService.reattach(sessionUser);
modelAndView.addObject("authenticatedUser", sessionUser);
}
}
Then from other place:
Code:
public List getAll() {
return getHibernateTemplate().loadAll(UserBean.class);
}
Full stack trace of any exception that occurs:
net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 4, of class: com.jff.bo.UserBean
org.springframework.orm.hibernate.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:333)
org.springframework.orm.hibernate.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:222)
org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:155)
org.springframework.orm.hibernate.HibernateTemplate.lock(HibernateTemplate.java:231)
com.jff.service.dao.hibernate.UserBeanDaoImpl.reattach(Unknown Source)
Name and version of the database you are using:
MySQL 4.X
I put UserBean object to HttpServletRequest session, and then reattach it (getHibernateTemplate().lock(user, LockMode.NONE);
But in some situations UserBean object is already attached to Hibernate session and i get NonUniqueObjectException.
So my question is how can i check is object is already attached (beside checking for LazyInit exception).