3

我尝试使用 NHibernate ORM 制作一个项目,有一次当我认为我完成了所有工作时,它给了我一个NHibernate.MappingException: No persister for exception 我读到问题可能是我没有添加程序集在配置文件中,但我这样做了,而且它也没有修复......

如果有人有一点时间,请帮助我解决问题。

这是用来添加新 Player 对象的代码

private void btnInsert_Click(object sender, EventArgs e)
        {
            Player playerData = new Player();
            SetPlayerInfo(playerData);

            using (ISession session = SessionFactory.OpenSession)
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        session.Save(playerData); // here it spits
                        transaction.Commit();
                        GetPlayerInfo();
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw ex;
                    }
                }
            }

        }

private void GetPlayerInfo()
        {
            using (ISession session = SessionFactory.OpenSession)
            {
                IQuery query = session.CreateQuery("FROM Player");
                IList<Player> pInfos = query.List<Player>();
                dgvDisplay.DataSource = pInfos;
            }
        }

private void SetPlayerInfo(Player playerData)
        {
            playerData.PlayerName = tbxName.Text;
            playerData.PlayerAge = Convert.ToInt32(tbxAge.Text);
            playerData.DOJ = Convert.ToDateTime(dtpDOJ.Text);
            playerData.BelongsTo = cmbBelongsTo.SelectedItem.ToString();
        }

这里是映射Player.hbm.xml代码

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
  <class name="NHibernateExperiment.Player, NHibernateExperiment" lazy="true">
    <id name="PlayerId">
      <generator class="native"/>
    </id>
    <property name="PlayerName" column ="PlayerName"/>
    <property name="PlayerAge" column ="PlayerAge"/>
    <property name="DOJ" column="DOJ"/>
    <property name="BelongsTo" column="BelongsTo"/>
  </class>

</hibernate-mapping>

这是App.config代码

    <configuration>
  <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="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Server=GRITCAN;database=testDB;Integrated Security=SSPI;</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <property name="show_sql">true</property>          
      <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>          
    </session-factory>
  </hibernate-configuration>
</configuration>

这是堆栈跟踪

在 NHibernateExperiment.Form1.btnInsert_Click(Object sender, EventArgs e) 在 E:\projects\tests\NHibernate\NHibernateExperiment\NHibernateExperiment\Form1.cs: 系统中 System.Windows.Forms.Control.OnClick(EventArgs e) 的第 72 行。 Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows。 Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage( Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(Form mainForm ) 在 E:\projects\tests\NHibernate\NHibernateExperiment\NHibernateExperiment\Program.cs:第 16 行中的 NHibernateExperiment.Program.Main() System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly (字符串 assemblyFile,证据 assemblySecurity,String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart()对象状态)在 System.Threading.ThreadHelper.ThreadStart()对象状态)在 System.Threading.ThreadHelper.ThreadStart()

我在 NHibernate.dllNHibernate.ByteCode.LinFu.dll项目中添加了以下 2 个引用

非常感谢你的帮助!


谢谢你们。.hbm.xml文件的构建操作Content。正如你建议我的那样,我将其更改为嵌入式资源并且一切正常:)

4

3 回答 3

3

您是否使映射文件成为嵌入式资源?

于 2012-06-27T13:38:57.057 回答
3

检查 xml 映射是否被标记为嵌入资源。此外,我建议您使用 Fluent nHibernate 库 - 这是一种无需编写大量 xml 映射的自由,只有 .net 类

于 2012-06-27T13:45:15.270 回答
0

工作解决方案:

Form1.cs 使用 NHibernate;使用 NHibernate.Cfg; 使用系统;使用 System.Collections.Generic;使用 System.ComponentModel;使用 System.Data;使用 System.Drawing;使用 System.Linq;使用 System.Text;使用 System.Threading.Tasks;使用 System.Windows.Forms;

namespace NHibernateTutorialPart1
{
    public partial class Form1 : Form
    {

        private Configuration myConfiguration;
        private ISessionFactory mySessionFactory;
        private ISession mySession;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myConfiguration = new Configuration();
            myConfiguration.Configure("hibernate_mysql.cfg.xml");
            mySessionFactory = myConfiguration.BuildSessionFactory();
            mySession = mySessionFactory.OpenSession();

            using (mySession.BeginTransaction())
            {
                Contact lbContact=new Contact{FirstName="Nisha", LastName="Shrestha",ID=0};
                mySession.Save(lbContact);
                mySession.Transaction.Commit();
            }
        }
    }
}

**hibernate_mysql.cfg.xml**

<?xml version="1.0" encoding="utf-8" ?>
<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">
      User Id=root;
      Server=localhost;
      Password=Password1;
      Database=nhibernatecontacts;
    </property>

    <!--This is good for Debugging but not otherwise-->
    <property name="show_sql">true</property>

    <!--<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>-->
    <mapping assembly="NHibernateTutorialPart1"/>
  </session-factory>
</hibernate-configuration>


**contact.hbm.xml**

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="NHibernateTutorialPart1"
                   namespace="NHibernateTutorialPart1">
  <class name="Contact" table="contact">
    <id name="ID" column="ID">
      <generator class="identity" />
    </id>
    <property name="FirstName" />
    <property name="LastName" />
  </class>
</hibernate-mapping>

**Contact class**


namespace NHibernateTutorialPart1
{
    public class Contact
    {

        public virtual int ID { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }

    }
}


**App.config**

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
  </configSections>
</configuration>

You need to add reference for lesi.collection and NHibernate
You need to do embeded resource for contact.hbm.xml and hibernate_mysql.cfg.xml

我使用了 mysql 工作台,在其中创建了一个新模式和一个带有 id、名字和姓氏的新表。

No persister for exception 的解决方案是我忘记在 contact.hbm.xml 中给出表名

于 2014-01-24T16:55:10.610 回答