I'm trying to use Hibernate in a session ejb in Oracle oc4j.
I've mapped the Cat class (no relationships)
and create/read/update/delete all work fine
in a java application using one Hibernate session.
In the session ejb, I get the Hibernate SessionFactory in
setSessionContext, then use it (or try to)
in the following remote methods:
List read(String hql)
Cat create(Cat cat)
void delete(Cat cat)
void update(Cat cat)
The read and create methods work great.
For delete and update I pass back to the ejb one of the Cats
previously found by read. These methods fail with the following
message:
04/01/05 12:10:40 Hibernate: delete from CAT where id=?
12:10:40,303 ERROR SessionImpl:2269 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)
I've tried doing session.load() before deleting - no go.
I'm sure I'm missing something basic ... can anyone point me to a clear
example of how this SHOULD work?
TIA,
Sandy
Code:
public abstract class CatSFBean implements SessionBean {
SessionContext c = null;
SessionFactory sessionFactory = null;
public void setSessionContext(SessionContext ctx) {
System.out.println("In setSessionContext()");
this.c = ctx;
try {
this.sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException he) {
he.printStackTrace();
}
}
public void unsetSessionContext() {
System.out.println("In unsetSessionContext()");
this.c = null;
}
public Cat deleteCat(Cat aCat) throws HibernateException {
Session session;
Transaction transaction;
session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(aCat);
tx.commit();
}
catch (HibernateException e) {
if (tx != null)
tx.rollback();
throw e;
}
finally {
session.close();
}
return aCat;
}
}