-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Generic DAO implementation not working.. what i´m missing ?
PostPosted: Tue Feb 27, 2007 2:28 pm 
Beginner
Beginner

Joined: Wed Mar 22, 2006 6:59 am
Posts: 30
Hi, i have this "session manager":

Code:
    public class SessionManager
    {

        private bool _initialized = false;
        private ISessionFactory _factory;


        /// <summary>
        /// Returns true if the SessionSource has been initialized successfully.
        /// </summary>
        public bool IsInitialized
        {
            get { return _initialized; }
        }

        private static SessionManager _current = new SessionManager();
        public static SessionManager Current
        {
            get { return _current; }
        }

        private SessionManager()
        {
            //prevent direct creation
        }

        /// <summary>
        /// Reads the NHibernate configuration and builds the session factory.
        /// </summary>
        public void Initialize()
        {
            Configuration cfg = new Configuration();
           
            _factory = cfg.Configure().BuildSessionFactory();

            _initialized = true;
        }

        /// <summary>
        /// Closes the current session factory.  The next call
        /// to initialize or GetSession will re-initialize the SessionSource
        /// </summary>
        public void Close()
        {
            if (_factory == null)
                return;

            _factory.Close();
            _initialized = false;
        }

        public ISession GetSession()
        {
            //if we aren't initialized, do it
            if (!_initialized)
                Initialize();

            return _factory.OpenSession();
        }

        public ISession GetSession(IDbConnection connection)
        {
            //if we aren't initialized, do it
            if (!_initialized)
                Initialize();

            return _factory.OpenSession(connection);
        }

        public ISession GetSession(IInterceptor interceptor)
        {
            if (!_initialized)
                Initialize();

            return _factory.OpenSession(interceptor);
        }

    }


In my tests, when i use it directly, my data is saved correctly. see :


Code:
           ISession session = SessionManager.Current.GetSession();
           
            User user = new User();
            user.Name = "Paulo";
            user.Password = "masterkey";
            user.Login = "Paulo2";

            Assert.AreEqual(0, user.Id);

            ITransaction trans = session.BeginTransaction();
            session.SaveOrUpdate(user);
            trans.Commit();


But, when i try to use my "sesison manager" in a generic DAO, my data is not saved. For generation id i´m using a Generator object from Firebird database... and its geting incremented, but no data on table is saved.

here is my DAO, i think i´m missing something here... please if you can, point it out.

IGeneric DAO
Code:
    public interface IGenericDAO<T, ID>
    {
        T GetById(ID id, bool shouldLock);
        List<T> GetAll();
        List<T> GetByExample(T exampleInstance, params string[] propertiesToExclude);
        T GetUniqueByExample(T exampleInstance, params string[] propertiesToExclude);
        T SaveOrUpdate(T entity);
        void Delete(T entity);

    }


"My" Generic NHibernate DAO:

Code:
    public abstract class GenericNHibernateDAO<T, ID> : IGenericDAO<T, ID>
    {
        /// <summary>
        /// Exposes the ISession used within the DAO.
        /// </summary>
        private ISession session
        {
            get
            {
                return SessionManager.Current.GetSession();
            }
        }

        /// <summary>
        /// Loads an instance of type T from the DB based on its ID.
        /// </summary>
        public T GetById(ID id, bool shouldLock)
        {
            T entity;

            if (shouldLock)
            {
                entity = (T)session.Load(persitentType, id, LockMode.Upgrade);
            }
            else
            {
                entity = (T)session.Load(persitentType, id);
            }

            session.Close();

            return entity;
        }

        /// <summary>
        /// Loads every instance of the requested type with no filtering.
        /// </summary>
        public List<T> GetAll()
        {
            return GetByCriteria();
        }

        /// <summary>
        /// Loads every instance of the requested type using the supplied <see cref="ICriterion" />.
        /// If no <see cref="ICriterion" /> is supplied, this behaves like <see cref="GetAll" />.
        /// </summary>
        public List<T> GetByCriteria(params ICriterion[] criterion)
        {
            ICriteria criteria = session.CreateCriteria(persitentType);

            foreach (ICriterion criterium in criterion)
            {
                criteria.Add(criterium);
            }

            session.Close();

            return ConvertToGenericList(criteria.List());
        }

        public List<T> GetByExample(T exampleInstance, params string[] propertiesToExclude)
        {
            ICriteria criteria = session.CreateCriteria(persitentType);
            Example example = Example.Create(exampleInstance);

            foreach (string propertyToExclude in propertiesToExclude)
            {
                example.ExcludeProperty(propertyToExclude);
            }

            criteria.Add(example);

            session.Close();

            return ConvertToGenericList(criteria.List());
        }

        /// <summary>
        /// Looks for a single instance using the example provided.
        /// </summary>
        /// <exception cref="NonUniqueResultException" />
        public T GetUniqueByExample(T exampleInstance, params string[] propertiesToExclude)
        {
            List<T> foundList = GetByExample(exampleInstance, propertiesToExclude);

            if (foundList.Count > 1)
            {
                throw new NonUniqueResultException(foundList.Count);
            }

            if (foundList.Count > 0)
            {
                return foundList[0];
            }
            else
            {
                return default(T);
            }
        }

        public T SaveOrUpdate(T entity)
        {
            ITransaction transaction = session.BeginTransaction();
           
            session.SaveOrUpdate(entity);

            transaction.Commit();

            session.Close();
           
            return entity;
        }

        public void Delete(T entity)
        {
            ITransaction transaction = session.BeginTransaction();
            session.Delete(entity);
            transaction.Commit();
            session.Close();
        }


        /// <summary>
        /// Accepts a list of objects and converts them to a strongly typed list.
        /// </summary>
        /// <remarks>This should be put into a utility class as it's not directly
        /// related to data access.</remarks>
        private List<T> ConvertToGenericList(System.Collections.IList listObjects)
        {
            List<T> convertedList = new List<T>();

            foreach (object listObject in listObjects)
            {
                convertedList.Add((T)listObject);
            }

            return convertedList;
        }

        private Type persitentType = typeof(T);
    }


My entity dao:

Code:
public interface IUserDAO : IGenericDAO<User, long> { };


Code:
public class UserDaoNHibernate : GenericNHibernateDAO<User, long>, IUserDAO { }


And also have a factory for returning this dao...

Code:
    public interface IDaoFactory
    {
        IUserDAO GetUserDao();
    }


Code:
    public class NHibernateDaoFactory : IDaoFactory
    {
        public IUserDAO GetUserDao()
        {
            return new UserDaoNHibernate();
        }

    }


To use all that i do:

Code:
            IDaoFactory daoFactory = new NHibernateDaoFactory();
            IUserDAO userDao = daoFactory.GetUserDao();

            User user = new User();
            user.Name = "Paulo";
            user.Password = "masterkey";
            user.Login = "Paulo2";

            userDao.SaveOrUpdate(user); //-> here my data is not saved, but firebird generator is incremented...



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.