I am having a horrible time with hibernate updating the database of an existing record. I am using struts, and have an action form and a value object. When I first open a record from the database, I create my value object. Here is my code in my DAO for getting the record.
Code:
Incident incident = (Incident) HibernateUtil.getSession().load(Incident.class, rbNumber);
Victim victim = (Victim) HibernateUtil.getSession().createQuery(
"from org.dotcomm.opd.valueobjects.Victim victim where victim.rbNumber = :rbNumber").setString("rbNumber", incident.getRbNumber())
.uniqueResult();
MissingPerson missingPerson = (MissingPerson) HibernateUtil.getSession().createQuery(
"from org.dotcomm.opd.valueobjects.MissingPerson mp where mp.victim = :victimObj").setEntity("victimObj", victim).uniqueResult();
Offense offense = (Offense) HibernateUtil.getSession().createQuery(
"from org.dotcomm.opd.valueobjects.Offense offense where offense.rbNumber = :rbNumber").setString("rbNumber", incident.getRbNumber())
.uniqueResult();
HibernateUtil.commitTransaction();
I then create my action form for struts from this value object, let the user update the information, and then when they hit save, I update the value object. I then try to save the value object using this code.
Code:
HibernateUtil.getSession().update(missingPerson.getIncident());
HibernateUtil.getSession().update(missingPerson.getOffense());
//HibernateUtil.getSession().update((NCICInformation)missingPerson.getOffense().getNcicInformations().iterator().next());
HibernateUtil.getSession().update(missingPerson);
HibernateUtil.getSession().update(missingPerson.getVehicle());
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
It throws the Exception org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions
on the first update of the line. I understand that I created a session when I queried the objects. All I want to do is update them. How do I clear out the old session, or re-use it, or what do I need to do here?
If I close the session after the query, I get a lazyloading exception that a session doesn't exist. If I try to clear the session before updating the classes, it has no effect. Can someone tell me what I am missing? Thanks.