let us start using struts.\rand some fixes in the new persistence layer.
[mir.git] / source / mir / core / service / storage / StorageService.java
index efa2e50..6a21d94 100755 (executable)
@@ -34,7 +34,9 @@ package mir.core.service.storage;
 
 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;
@@ -45,24 +47,24 @@ import net.sf.hibernate.expression.Expression;
  * 
  * 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){
@@ -73,32 +75,39 @@ public abstract class StorageService {
                                .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);
@@ -107,10 +116,10 @@ public abstract class StorageService {
        
        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);
                }                       
@@ -118,13 +127,17 @@ public abstract class StorageService {
        
        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();       
+       }
 }