Hm, OK, what I was doing was OK, the problem is that I have 2 calls to my logic object and I am getting an error;
ScheduleLogic.loadSchedule(Unknown Source) : a different object with the same identifier value was already associated with the session: 2, of class:
I have 2 test calls to 2 separate methods in my logic object
Code:
request.setAttribute("aSchedule", logic.loadSchedule(2));
and
Code:
Schedule schedule2 = new DataAcquisitionSchedule();
logic.loadSchedule(schedule2, 2);
I copy the code for these methods below. As far as I can see, they are 2 totally separate calls to methods so why is this causing the error? Is it that the currentSession is already in use when the second call goes in? If I change the first method call to 1 both statements work.
I would probably never need to load the same object sequentially like this, but I would like to understand what is happening here as it seems to me that this cannot be right - that is, surely a sequence of operations on 1 row is quite common??
Code:
/**
* Retrive a Schedule from ID.
*
*/
public Schedule loadSchedule(int id) {
Schedule schedule = null;
try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
schedule = (Schedule) session.load(Schedule.class, new Integer(id));
tx.commit();
} catch (HibernateException hE) {
logger.error(hE.getMessage());
}
return schedule;
}
/**
* Retrive a Schedule into a passed Schedule.
*
*/
public void loadSchedule(Schedule schedule, int id) {
try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.load(schedule, new Integer(id));
tx.commit();
} catch (HibernateException hE) {
logger.error(hE.getMessage());
}
}
Thanks guys, ADC.