cez*_*ann 1 c# nhibernate persistence nhibernate-mapping
好吧,我用谷歌搜索了很多,发现了相同的建议(将 hbm 设置为嵌入式资源,在 hibernate.cfg 中添加 hbm 等),尽管如此,我仍然不明白。
让我解释一下:我为检票口设备编写了一个通信 Dll,其中有一个配置模型类,我用它通过 TCP/IP 配置该设备。但现在,我必须将这个对象持久保存在数据库上,因此我在模型中编写了一个适配器,将一个对象粘合到另一个对象上。这样,我就有了一个适用于我的配置模型的适配器,其中包含 ID、InclusionDate 等。让我们来看看:
DeviceConf 模型类:
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.
}
Run Code Online (Sandbox Code Playgroud)
DeviceConf.hbm.xml 映射文件:
< 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>
Run Code Online (Sandbox Code Playgroud)
保存方法:
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用我这里的另一个模型类,一切都运行良好,但不适用于这个。每次我尝试使用 Create 方法保存此内容时,NHibernate 都会引发 MappingException 并显示“TGPass.Model.DeviceConf 没有持久器”...
我哪里做错了?
先感谢您。
为了完整起见(也是基于我的痛苦经历),这个异常的原因主要有三个:
Embedded Resource<mapping assembly="MyProject.Data" /> (请参阅<session-factory>配置).hbm.xml其中之一通常是以下问题的罪魁祸首:MappingException:没有持久化器...