import java.util.List;
+import mir.core.model.IImage;
import net.sf.hibernate.Criteria;
+import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
*
* StorageService
* @author idefix
- * @version $Id: StorageService.java,v 1.1 2003/08/17 19:11:49 idfx Exp $
+ * @version $Id: StorageService.java,v 1.3 2003/09/05 20:23:59 idfx Exp $
*/
public abstract class StorageService {
private Class objectClass;
- private SessionFactory factory;
-
+ private SessionHolder sessionHolder;
+
public StorageService(Class objectClass, SessionFactory factory){
this.objectClass = objectClass;
- this.factory = factory;
+ sessionHolder = new SessionHolder(factory);
}
-
+
public List list(int offset, int limit){
return list(offset, limit, null);
}
public List list(int offset, int limit, Expression expression){
try {
- Session session = factory.openSession();
+ Session session = sessionHolder.currentSession();
Transaction transaction = session.beginTransaction();
Criteria criteria = session.createCriteria(objectClass);
if(expression != null){
.setMaxResults(limit);
List returnList = criteria.list();
transaction.commit();
- session.close();
+ sessionHolder.closeSession();
return returnList;
} catch (HibernateException e) {
throw new StorageServiceFailure(e);
- }
+ }
}
public Object load(Integer id){
try {
- Session session = factory.openSession();
+ Session session = sessionHolder.currentSession();
Transaction transaction = session.beginTransaction();
Object returnObject = session.load(objectClass, id);
+ initializeLazyCollections(returnObject);
transaction.commit();
- session.close();
+ sessionHolder.closeSession();
return returnObject;
} catch (HibernateException e) {
throw new StorageServiceFailure(e);
}
}
+ /**
+ * @param returnObject
+ */
+ protected abstract void initializeLazyCollections(Object returnObject)
+ throws HibernateException;
+
public Integer add(Object newObject){
try {
- Session session = factory.openSession();
+ Session session = sessionHolder.currentSession();
Transaction transaction = session.beginTransaction();
Integer newid = (Integer)session.save(newObject);
- session.close();
+ transaction.commit();
return newid;
} catch (HibernateException e) {
throw new StorageServiceFailure(e);
public void update(Object toUpdate){
try {
- Session session = factory.openSession();
+ Session session = sessionHolder.currentSession();
Transaction transaction = session.beginTransaction();
session.update(toUpdate);
- session.close();
+ transaction.commit();
} catch (HibernateException e) {
throw new StorageServiceFailure(e);
}
public void delete(Object toDelete){
try {
- Session session = factory.openSession();
+ Session session = sessionHolder.currentSession();
Transaction transaction = session.beginTransaction();
session.delete(toDelete);
- session.close();
+ transaction.commit();
} catch (HibernateException e) {
throw new StorageServiceFailure(e);
}
}
+ public void finalize() throws Throwable {
+ sessionHolder.closeSession();
+ super.finalize();
+ }
}