I don't know where is error.
I use NHibernate C# and append Assembly to Configuration. Problem a NHibernateException: Could not compile the mapping Note.hbm.xml
public static class NHibernateHelper
{
private static Configuration _cfg;
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory {
get {
if (_sessionFactory == null) {
_cfg = new Configuration();
_cfg.Configure();
_cfg.AddAssembly(typeof(User).Assembly);
_cfg.AddAssembly(typeof(Changelog).Assembly);
_cfg.AddAssembly(typeof(Note).Assembly);
_sessionFactory = _cfg.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession() {
return SessionFactory.OpenSession();
}
public static void DatabaseSchemaLoader() {
ISession session = OpenSession();
new SchemaExport(_cfg).Create(true, true);
session.Close();
}
}
My Note class is shared.data.content
namespace CRMSystem.shared.data.content
{
public class Note
{
public virtual int id { get; set; }
public virtual string title{ get; set;}
public virtual string text { get; set; }
public virtual int priority { get; set; }
public virtual long createdTime { get; set; }
public virtual long lastEditTime { get; set; }
}
}
Note.hbm.xml (set build action: embedded resource, copy to output: copy always)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="CRMSystem"
namespace="CRMSystem.shared.data.content">
<class name="Note" table="`Note`" lazy="false">
<id name="id" column="`id`" type="int">
<generator class="identity"></generator>
</id>
<property name="title" column="`title`" type="String"/>
<property name="text" column="`text`" type="String" />
<property name="priority" column="`priority`" type="int" />
<property name="createdTime" column="`createdTime`" type="long" />
<property name="lastEditTime" column="`lastEditTime`" type="long" />
</class>
</hibernate-mapping>
Thanks for your answer.
I have tried today in many ways and found to that it works:
_cfg.Configure();
_cfg.AddAssembly(typeof(User).Assembly);
//_cfg.AddAssembly(typeof(Changelog).Assembly);
//_cfg.AddAssembly(typeof(Note).Assembly);
When I adds: (any class)
_cfg.AddAssembly(typeof(Changelog).Assembly);
Every I get an error: Could not compile Note.hbm.xml I can save to database every class (User, Changelog, Note). I can't explain.
Has anyone ever have such situations ?
Full exception:
NHibernate.MappingException: Could not compile the mapping document: CRMSystem.shared.data.mapping.Note.hbm.xml ---> NHibernate.DuplicateMappingException: Duplicate class/entity mapping CRMSystem.shared.data.content.Note
w NHibernate.Cfg.Mappings.AddClass(PersistentClass persistentClass)
w NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas)
w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas)
w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas)
w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema)
w NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
--- Koniec śladu stosu wyjątków wewnętrznych ---
w NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
w NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
w NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
w NHibernate.Cfg.Configuration.ProcessMappingsQueue()
w NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
w NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
w NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
w NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
w NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
When I nothing add , i get exception : no persister
EXTEND
Based on the latest update, we can see, that NHiberante is blaming at:
And that means, that in our case, some other file could contain the NOTE mapping, e.g. in user.hbm.xml there could be some forgotten mapping
ORIGINAL part
The code is ok as is. I tried locally with your
Note
class andNote.hbm.xml
. Working as expected.The issue seems to be some misspelling which I can hardly reproduce (e.g. different assembly name). Or, despite of the blame on the Note.hbm.xml ... it also could be some other mapping
Anyhow, try to observe the INNER exception. There will be clear issue description. E.g. for this mapping
We would receive full exception:
Note: embedded resource property is must, do not copy is suggested. This file will be part of dll, it does not have to be copied as a file.