MappingException:没有持久性-NHibernate-持久化适配器 [英] MappingException: No persister for - NHibernate - Persisting an Adapter

查看:86
本文介绍了MappingException:没有持久性-NHibernate-持久化适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在Google上搜索了很多,并在那里找到了相同的建议(将hbm设置为Embedded Resource,在hibernate.cfg上添加hbm,依此类推.),尽管有这些建议,我仍然没有得到.

Well, I googled a lot, and found the same advices all there (set hbm as Embedded Resource, add hbm at hibernate.cfg, etc..), and despite of them, I still not get it.

让我解释一下:我为检票口设备编写了一个Communication Dll,那里有一个配置模型类,可用于通过TCP/IP配置该设备.但是现在,我必须将此对象保留在DB上,因此我在模型内部编写了一个Adapter,将一个适配器粘在一起.这样,我为我的配置模型提供了一个适配器,该适配器具有一个ID,IncludedDate等.让我们看看:

Let me explain: I wrote a Communication Dll for a ticket gate device, and there I have a configuration model class, that I use to configure that device through TCP/IP. But now, I have to persist this object on DB, so I wrote an Adapter inside my model which glues one into another. This way, I have an Adapter for my configuration model that have an ID, IncludedDate and so on. Let's see:

DeviceConf模型类:

DeviceConf Model class:

public class DeviceConf : BaseModel // which have ID, IncludedDate, etc
{
    private TGCommHelper.Entities.Configuration.TicketGateConfig _conf;

    public TGCommHelper.Entities.Configuration.TicketGateConfig conf
    {
        get { return _conf; }
        private set { _conf = value; }
    }
    public DeviceConf()
    {
        conf = new TGCommHelper.Entities.Configuration.TicketGateConfig();
    }
    public DeviceConf(TGCommHelper.Entities.Configuration.TicketGateConfig config){
        conf = config;
    }

    public virtual string IP
    {
        get { return conf.IP; }
        set { conf.IP = value; }
    }

    public virtual string MAC_ADDR
    {
        get { return conf.MAC_ADDR; }
        set { conf.MAC_ADDR = value; }
    }

//... and so on.
}

DeviceConf.hbm.xml映射文件:

DeviceConf.hbm.xml Mapping file:

    < hibernate-mapping assembly="TGPass.Model" namespace="TGPass.Model"
xmlns="urn:nhibernate-mapping-2.2">
    < class name="DeviceConf" table="DeviceConfTbl">
        < id name="ID" column="ID">
            < generator class="identity" />
        < /id>
        < property name="IP">
            < column name="IP" sql-type="varchar" not-null="true" />
        < /property>
        < property name="MAC_ADDR">
            < column name="MAC_ADDR" sql-type="varchar" not-null="true" />
        < /property>
        < !-- and so on -->
    < /class>
< /hibernate-mapping>

保存方法:

public virtual void Create(T saveObj)
{
    using (var session = GetSession())
    {
        using (var trans = session.BeginTransaction())
        {
            try
            {
                session.Save(saveObj);
                trans.Commit();
            }
            catch (Exception e)
            {

                throw e;
            }
        }
    }
}

我在这里有另一个模型类,所有的东西都能很好地工作,但是在这个模型中却不能. 每次我尝试使用Create方法保存它时,NHibernate都会引发一个MappingException,其中显示"TGPass.Model.DeviceConf没有持久性" ...

With another model classes I have here, all things work nicely, but not with this one. Every time I try to save this with Create method, NHibernate raises a MappingException with "No persister for TGPass.Model.DeviceConf"...

我在哪里做错了?

谢谢.

推荐答案

出于完整性的考虑((同样基于我的痛苦经历) ),出现此异常的原因主要有三个:

Just for completeness (also based on my painful experience), there are mostly three reasons of this exception:

  1. xml映射文件未标记为 Embedded Resource
  2. xml文件不是配置为映射源<mapping assembly="MyProject.Data" /> .dll 的一部分(请参见
  1. xml mapping file is NOT makred as Embedded Resource
  2. xml file is not part of .dll which is configured as the mapping source <mapping assembly="MyProject.Data" /> (see <session-factory> configuration)
  3. xml file does not have the default suffix .hbm.xml

其中之一通常是以下原因的罪魁祸首: MappingException:没有持久性......

One of these is usually the culprit of the: MappingException: No persister for...

这篇关于MappingException:没有持久性-NHibernate-持久化适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆