MappingException: No persister for - NHibernate -

2019-02-27 16:03发布

问题:

This question already has an answer here:

  • NHibernate.MappingException: No persister for: XYZ 15 answers

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.

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 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 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>

Save Method:

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;
            }
        }
    }
}

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"...

Where I'm doing it wrong?

Thank you in advance.

回答1:

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

  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

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