ASP.net NHibernate没有找到Persister

时间:2012-01-15 20:36:54

标签: asp.net .net nhibernate asp.net-4.0

我创建了一个ASP.net 4.0项目,我想使用NHibernate连接到我的localhost上的MySQL数据库。但由于这对我来说是新的,我遵循了一个指南(link

大多数解决方案需要嵌入xml文件,但我不知道如何在ASP项目中执行此操作。

我完成了那里描述的所有内容,也下载了项目,但现在我有以下错误:

No Persister for: MediaCatalog.Model.Movie

此行发生错误:

ISession.SaveOrUpdate(movie);

堆栈跟踪:

  

at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)      at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName,Object obj)      在NHibernate.Engine.ForeignKeys.IsTransient(字符串entityName,Object实体,Nullable`1假设,ISessionImplementor会话)      at NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object entity,String entityName,EntityEntry entry,ISessionImplementor source)      在NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent事件)      在NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent事件)      在NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent事件)      在NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj)      在MediaCatalog.DAO.MovieDAO.create(电影电影)中的c:\ Users \ Anthony \ Documents \ Visual Studio 2010 \ WebSites \ MediaCatalog \ App_Code \ DAO \ MovieDAO.cs:第74行      位于c:\ Users \ Anthony \ Documents \ Visual Studio 2010 \ WebSites \ MediaCatalog \ App_Code \ BLL \ MovieService.cs中的MediaCatalog.BLL.MovieService.getMoviesFromDirectory(String目录):第35行      at Default2.btnScanMovies_Click(Object sender,EventArgs e)位于c:\ Users \ Anthony \ Documents \ Visual Studio 2010 \ WebSites \ MediaCatalog \ Settings.aspx.cs:第25行      在System.Web.UI.WebControls.Button.OnClick(EventArgs e)      在System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)      在System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)      在System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl,String eventArgument)      在System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)      在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)

一些可能有用的代码片段:

的Web.config:

  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.connection_string">Server=localhost;Database=mediacatalog;uid=admin;pwd=admin;</property>
      <property name="current_session_context_class">web</property>
    </session-factory>
  </hibernate-configuration>

Movie.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MediaCatalog"
                   namespace="MediaCatalog.Model">
  <class name="MediaCatalog.Model.Movie, MediaCatalog" table="movie" lazy="true">
    <id name="Id" Column="ID">
      <generator class="guid"/>
    </id>
    <property name="Title" Column="Title"/>
    <property name="Year" Column="Year"/>
    <property name="Size" Column="Size"/>
  </class>
</hibernate-mapping>

Movie.cs类

namespace MediaCatalog.Model
{
    public class Movie
    {
        int Id;
        string Title;
        string Year;
        string Size;

        public Movie(String _title, string _year, string _size)
        {
            Title = _title;
            Year = _year;
            Size = _size;
        }

        #region Getters & Setters
        //GETTERS & SETTERS
        public int getId()
        {
            return Id;
        }

        public void setId(int _id)
        {
            this.Id = _id;
        }

        public String getTitle()
        {
            return Title;
        }

        public void setTitle(String title)
        {
            this.Title = title;
        }

        public String getYear()
        {
            return Year;
        }

        public void setYear(String year)
        {
            this.Year = year;
        }

        public String getSize()
        {
            return Size;
        }

        public void setSize(string size)
        {
            this.Size = size;
        }
        # endregion
    }
}

MovieDAO.cs类

namespace MediaCatalog.DAO
{
    public class MovieDAO : IMovieDAO
    {
        //private ISessionFactory _sessionFactory;
        //private Configuration _configuration;
        protected ISession _ISession;
        private Dictionary<string, FetchMode> _fetchModeMap = new Dictionary<string, FetchMode>();

        #region Properties
        public System.Type Type
        {
            get { return typeof(Movie); }
        }

        public ISession ISession
        {
            get { return _ISession; }
        }
        #endregion

        #region Constructors
        public MovieDAO(ISession session)
        {
            this._ISession = session;
        }
        #endregion

        #region Misc Methods
        public void SetFetchMode(string associationPath, FetchMode mode)
        {
            if (!_fetchModeMap.ContainsKey(associationPath))
            {
                _fetchModeMap.Add(associationPath, mode);
            }
        }

        public ICriteria CreateCriteria()
        {
            ICriteria criteria = ISession.CreateCriteria(typeof(Movie));

            foreach (var pair in _fetchModeMap)
            {
                criteria = criteria.SetFetchMode(pair.Key, pair.Value);
            }
            return criteria;
        }
        #endregion

        public void create(Movie movie)
        {
            //NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
            //config.AddAssembly("MediaCatalog");
            //_sessionFactory = config.BuildSessionFactory();

            //using (ISession session = _sessionFactory.OpenSession())
            //{
            //    using (session.BeginTransaction())
            //    {
            //        session.SaveOrUpdate(movie);
            //        session.Transaction.Commit();
            //        session.Close();
            //    }
            //}
            ISession.SaveOrUpdate(movie);
        }
    }
}

DataAccessFactory.cs类

namespace MediaCatalog.Factories
{
    public class DataAccessFactory
    {
        public static IMovieDAO LoadMovieDAO()
        {
            ISession session = ISessionManager.Instance.CreateISession();
            return new MovieDAO(session);
        }
    }
}

非常感谢帮助:)

1 个答案:

答案 0 :(得分:0)

你必须告诉NHibernate将哪些类或哪些程序集定义在这些类中以进行映射。你可以用:

来做到这一点
<mapping assembly="MediaCatalog" />

将它放在Web.Config中,并确保将* .hbm.xml文件设置为构建为嵌入式资源。