2

这是我的 AbstractNHibernateDao,我想将 SaveOrUpdateCopy 与 Merge 命令一起使用,但是当我尝试与 Type TI 一起使用时出现以下错误

错误 1 ​​类型“T”必须是引用类型才能在泛型类型或方法“NHibernate.ISession.Merge(T)”中用作参数“T”

public abstract class AbstractNHibernateDao<T, TIdT> : IDao<T, TIdT>
    {
        private readonly Type _persitentType = typeof (T);

        /// <summary>
        ///   Exposes the ISession used within the DAO.
        /// </summary>
        private static ISession NHibernateSession
        {
            get { return NHibernateSessionManager.Instance.GetSession(); }
        }

        #region IDao<T,TIdT> Members



        /// <summary>
        ///   For entities that have assigned ID's, you must explicitly call Save to add a new one. See http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-id-assigned.
        /// </summary>
        public T Save(T entity)
        {
            NHibernateSession.Save(entity);
            NHibernateSession.Flush();
            return entity;
        }

        /// <summary>
        ///   For entities with automatatically generated IDs, such as identity, SaveOrUpdate may be called when saving a new entity. SaveOrUpdate can also be called to update any entity, even if its ID is assigned.
        /// </summary>
        public T SaveOrUpdate(T entity)
        {
            NHibernateSession.SaveOrUpdate(entity);
            NHibernateSession.Flush();
            return entity;
        }

        public T SaveOrUpdateCopy(T entity)
        {
            NHibernateSession.Merge(entity);
            return entity;
        } 

        public void Delete(T entity)
        {
            NHibernateSession.Delete(entity);
            NHibernateSession.Flush();
        }

        /// <summary>
        ///   Commits changes regardless of whether there's an open transaction or not
        /// </summary>
        public void CommitChanges()
        {
            if (NHibernateSessionManager.Instance.HasOpenTransaction())
            {
                NHibernateSessionManager.Instance.CommitTransaction();
            }
            else
            {
                // If there's no transaction, just flush the changes
                NHibernateSessionManager.Instance.GetSession().Flush();
            }
        }

    }

我怎么能用它?

4

1 回答 1

3

你应该这样做Merge(entity)而不是Merge(T)

public abstract class AbstractNHibernateDao<T, TIdT> 
    : IDao<T, TIdT> where T : class
{
    public T SaveOrUpdateCopy(T entity)
    {
        NHibernateSession.Merge(entity);
        return entity;
    }
}
于 2012-08-08T09:39:59.263 回答