Thank you so much for your help. Right now, there is only one user - JUnit tests. It is considered incredibly unlikely that more than one user will ever access the app at once.
We've created a Saver class - a singleton - that we're using as a persistence proxy. It has a saveOrUpdate() method that we'd like to use to persist new objects and to update pre-existing (loaded) objects:
Code:
public void saveOrUpdate(Object o) throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(o);
session.flush();
tx.commit();
HibernateUtil.closeSession();
}
public Object get(Class c, String id) throws HibernateException {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Object o = session.get(c, id);
tx.commit();
HibernateUtil.closeSession();
return o;
}
That's our attempt at isolating our classes from Hibernate.
HibernateUtil.java is from the docs, with only changes to load mapping files from a directory:
Code:
/* HibernateUtil.java
package com.company.partyKeeper.persistence;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// MappingLoader reads files from mapping folder to add to a cfg.
MappingLoader ml = new MappingLoader();
// Create and load the Configuration
Configuration cfg = ml.fillConfiguration(new Configuration());
// Create the SessionFactory
sessionFactory = cfg.buildSessionFactory();
} catch (Exception ex) {
throw new RuntimeException("*** Configuration problem: " + ex.getMessage(),
ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null) s.close();
}
}
We are successfully saving objects with this code. We just hit a problem, though, where we call Saver.saveOrUpdate() after making some changes and the database is not showing the changes.
If what I've posted so far doesn't have any glaring problems, I can start extracting some control code to post. Right now that's a TestCase stting up an object graph of a dozen or so classes then calling a control class to act on three of those objects/classes. So I don't yet have a simple test case to show our problem.
Thanks again,
Scott