MappingException:没有持久性-NHibernate-持久化适配器 [英] MappingException: No persister for - NHibernate - Persisting an Adapter
问题描述
好吧,我在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:
- xml mapping file is NOT makred as
Embedded Resource
- xml file is not part of .dll which is configured as the mapping source
<mapping assembly="MyProject.Data" />
(see<session-factory>
configuration) - 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屋!