A solution, well not quite, but getting closer!
Another Exception I was getting when making various changes was:
Code:
Cause: org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a collection with two open sessions; nested exception is org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
I have managed to avoid the work around I found (above) but I still don't understand the root problem, however by changing the queries that retrieve the existing Entity from the database, and the query to save the changes I can avoid getting the duplication.
Making the following changes
Old code:
Code:
public void saveLog(Log log) {
getHibernateTemplate().merge(response);
}
// @NamedQuery(name="log.getLog", query="from Log l where l.id = :log_id")
//
public Log getLog(Integer log_id) {
org.hibernate.Query q = getSession().getNamedQuery("log.getLog");
q.setInteger("log_id", log_id);
q.setString("group_name", group);
return (Log) q.uniqueResult();
}
New code:
Code:
public void saveLog(Log log) {
getHibernateTemplate().saveOrUpdate(response);
}
public Log getLog(String group, Integer log_id) {
String query = "from Log l where l.id = ? ";
Object[] parameters = { log_id };
List<Log> logs = (List<Log>) getHibernateTemplate().find(query, parameters);
Log log = logs.iterator().next();
return log;
}
by making the above changes I can manipulate my Domain class Instance from my business tier, and the save the changes without getting the join table record duplication I was getting, however I am unable to use the merge operation (perhaps I am/was misusing this). I would like to understnad why I had the problem I did, was I actually using 2 sessions at the same time, which were not synchronized in any wat resulting in both performing acceptable actions. It appears to me that the changed way of querying is the crux to my fix, but it doesnt seem a whole one if i (or any other) developer could easily call merge() and cause problems again.
What was the root problem?
Is there a better way to address my problem?