1
votes

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

1
maybe remove single coute in column attribute Note.hbm.xmlDanielVorph
Firstly - Move YOUR answer into your question. Delete that answer. Secondly could you also append the full stack of inner Exception? As I did? There will be clear answerRadim Köhler
Could you show the full exception? Exactly after your cite Could not compile Note.hbm.xml - exactly that next line is saying clearly what is the problem. Observe my own answer and you will see such extract I created for you...Radim Köhler
I'm sorry. I had problem with print stack trace.Grzegorz S
Not a problem .. I extended the answer (and locally tested to get the same error) . Simply - one of your hbm.xml files contains <class name="Note"... search for it and you have a culpritRadim Köhler

1 Answers

1
votes

EXTEND

Based on the latest update, we can see, that NHiberante is blaming at:

... Duplicate class/entity mapping CRMSystem.shared.data.content.Note ...

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

<class name="Note" ...

ORIGINAL part

The code is ok as is. I tried locally with your Note class and Note.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

// there could be also some issue
_cfg.AddAssembly(typeof(User).Assembly);
_cfg.AddAssembly(typeof(Changelog).Assembly);

Anyhow, try to observe the INNER exception. There will be clear issue description. E.g. for this mapping

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               // instead of this
               // assembly="CRMSystem"
               // we have this
               assembly="WrongAssembly"
               namespace="CRMSystem.shared.data.content">

We would receive full exception:

NHibernate.MappingException: Could not compile the mapping document: ...Note.hbm.xml ---> NHibernate.MappingException: persistent class
CRMSystem.shared.data.content.Note, WrongAssembly not found ---> System.IO.FileNotFoundException: Could not load file or assembly 'WrongAssembly' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.

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.