问题
I'm trying to get NHibernate to work. I've got this class:
mm.k.Domain.Kampagne
(namespace/assembly is mm.k.Domain)
In another Visual Studio project (Assembly mm.k.Infrastructure) I got my Mapping files (in a Mappings directory), my hibernate.cfg.xml and some repositories.
Heres my mapping file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="mm.k.Domain"
namespace="mm.k.Domain">
<class name="Kampagne" table="Kampagner">
<id name="Id">
<generator class="identity" />
</id>
<property name="Navn" not-null="true" />
<property name="Logo" />
</class>
</hibernate-mapping>
When I'm configuring my session, I do this:
_configuration.AddAssembly(typeof(mm.k.Domain.Kampagne).Assembly);
And thats what doesn't work! When calling:
var test = session.Get<Kampagne>(kampagneId);
I get the following error: "No persister for: mm.k.Domain.Kampagne" Like it doesn't register the embedded mapping fild. Note that I have the build action on the mapping file set to Embedded Resource.
If I change the above line to:
_configuration.AddFile(@"fullpath\mm.k.Infrastructure\Mappings\Kampagne.hbm.xml");
Everything works perfectly fine!
Any ideas? Thanks in advance.
回答1:
Not sure what your nhibernate.cfg.xml file looks like, but I generally have an item like this
<mapping assembly="mm.K.Infrastructure"/>
based on your information you've given. NHibernate uses this to load the mapping files from this specific assembly.
This should give you the mapping you need.
回答2:
In case someone will have this issue with Hibernate.NET as I did. Make sure you selected in Properties Window for your file Build Action as "Embedded Resource".
回答3:
I was Getting the problem. But suddenly observed that the mapping file was not embedded. Goto the .hbm.xml file. Click properties. Then advanced -> Select "Embedded Resource"
回答4:
Whenever you use hbm.xml file you will set your configuration class like this:
Configuration cfg = new Configuration();
cfg.Configure();
// Add class mappings to configuration object
cfg.AddAssembly(Assembly.GetCallingAssembly());
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
Whenever you use Nhibernate.Mapping.Attributes like classe you will have to use: For example you have use Mapping.attributes in Product Class
Configuration cfg = new Configuration();
cfg.Configure();
// Add class mappings attributes to configuration object
cfg.AddInputStream(HbmSerializer.Default.Serialize(typeof(Model.Product);
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
来源:https://stackoverflow.com/questions/1436072/nhibernate-mappingexception-no-persister