nHibernate and mapping files.

Error: "Could not compile the mapping document"

Page 1 of 1

1 Replies - 9510 Views - Last Post: 24 June 2009 - 09:24 AM

#1 Methical   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-May 09

nHibernate and mapping files.

Posted 14 May 2009 - 01:00 AM

Hello again people,

I am trying to set up nHibernate on a winForm project in currently working on. But the issue I have when trying to run it is that i get a "Could not compile the mapping document: FollderSync_BestPractice_Model.DAO.Observation.hbm.xml"

Ill give you a brief run-down of how my project is structured as i find most of these issues often arise from incorrectly defining namespaces or assembly references. I have checked and changed these values in the mapping and config documents but still no luck. So here we go.

I am running SQL server 2005 and have a database called FileSyncDB, this DB has a table called tbObservation which has a load of properties in it...thrilling. Anyway the project. It would be quite alot of reading compared to hardly any effort just looking at my Solution Explorere so here it is in all it's glory.
Posted Image

Now for the dirty bits;

App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration"
    type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="dialect">
        NHibernate.Dialect.MsSql2005Dialect
      </property>
      <property name="connection.driver_class">
        NHibernate.Driver.SqlClientDriver
      </property>
      <property name="connection.connection_string">
        Server=TURDS-L56YGH2MS\SQLEXPRESS;
        Database=FolderSyncDB;
        Integrated Security=True;
      </property>
      <property name="show_sql">
        true
      </property>
    </session-factory>
  </hibernate-configuration>



And the infamous Observation.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="FolderSync_BestPractice_Model"
                   namespace="FolderSync_BestPractice_Model.DAO">
  
  <class name="Observation" table="tbObservation">
    
    <id name="ID" column="id">
      <generator class="native"/>
    </id>

    <property name ="Type" column="Type" length="13" not-null="true"/>
    <property name ="PreChange" column="PreChange" not-null="true"/>
    <property name ="PostChange" column="PostChange" not-null="true"/>
  </class>  
</hibernate-mapping>



So im fairly certian that i have got the namespace and assembly attributes correct, plus there is certainly a reference to the model in my console section. Just incase i could have created any stupid errors ill give you a look at the actual table this is supposed to map to.
Posted Image

Thrilling ! So here is what i am trying to do. In terms of the C# i am writing, i dont really know what i am doing. I know C# but i do not know nHibernate well, so all this talk about ISessions and ISessionFactory's is beyond me.

So here is my FolderSync_Console.Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using FolderSync_BestPractice_Model;
using FolderSync_BestPractice_Model.DAO;
using NHibernate;

namespace FolderSync_Console
{
    class Program
    {
        static void Main(string[] args)
        {
            runModel();
        }

        private static void runModel()
        {
            var cfg = new Configuration();
            cfg.AddAssembly(typeof(Observation).Assembly);

            using (ISessionFactory factory = cfg.BuildSessionFactory())
            {
                int obID = 0;
                ISession session = factory.OpenSession();

                Observation obs = new Observation();
                obs.Type = ObservationType.FileCreated.ToString();
                obs.PreChange = "CREATED";
                obs.PostChange = "J:\\Finch's Stuff\\SyncTest\\LookAt\\WorkHours.txt";

                Print("Saving Observation");

                session.Save(obs);
                session.Flush();

                obID = obs.ID;

                using (ISession anotherSession = factory.OpenSession())
                {
                    Print("Loading Observation");
                    Observation observation = anotherSession.Load<Observation>(obID);

                    Print("Observation ID: " + observation.ID);
                }
            }

            
            
            
        }

        private static void Print(string text)
        {
            Console.WriteLine(text);
        }
    }
}



Again, facinating. But it doesn't work !!!

I can tell you that the line
cfg.AddAssembly(typeof(Observation).Assembly);
is the line that causes all the hassle but from looking at similar posts on other forums, people seem to ask for a stack trace, so...
"at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 342\r\n   at 
NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 516\r\n   at 
NHibernate.Cfg.Configuration.ProcessMappingsQueue() in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 1661\r\n   at 
NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 1652\r\n   at 
NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 1645\r\n   at 
NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 571\r\n   at 
NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 609\r\n   at 
NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly) in 
c:\\CSharp\\NH\\nhibernate\\src\\NHibernate\\Cfg\\Configuration.cs:line 685\r\n   at 
FolderSync_Console.Program.runModel() in J:\\Finch's Stuff\\University Work\\Year 
3\\C#\\FolderSync_BestPractice\\FolderSync_Console\\Program.cs:line 22\r\n   at 
FolderSync_Console.Program.Main(String[] args) in J:\\Finch's Stuff\\University Work\\Year 
3\\C#\\FolderSync_BestPractice\\FolderSync_Console\\Program.cs:line 16\r\n   at 
System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n   at 
System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n   at 
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n   at 
System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n   at 
System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object 
state)\r\n   at System.Threading.ThreadHelper.ThreadStart()"



I actually have no idea what a stack trace is or why it is useful so i apologise for not leaving any kind of meaningful comment or description next to this one.

Just incase it matters ill chuck in my observation.cs as well.
using System;
using System.Collections.Generic;
using System.Text;

namespace FolderSync_BestPractice_Model.DAO
{
    public enum ObservationType
    {
        FolderCreated,
        FileCreated,

        FolderChanged,
        FileChanged,

        FolderRenamed,
        FileRenamed,

        FolderMoved,
        FileMoved,

        None
    }

    public class Observation
    {
        public virtual int ID { get; set; }
        public virtual String Type { get;  set; }
        public virtual string PreChange { get; set; }
        public virtual string PostChange { get; set; }

        public Observation()
        {

        }
    }
}



Thank you kindly for any information anyone may have to offer on this matter. As i say before i am totally new to nHibernate and ORM in general so this was only supposed to be a sample app hence it's simplicity but id like to get a simple sample to work before delving into the complications of large solutions.

Is This A Good Question/Topic? 0
  • +

Replies To: nHibernate and mapping files.

#2 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: nHibernate and mapping files.

Posted 24 June 2009 - 09:24 AM

The <class> property in the hbm-file should be formatted like this:
<class name="namespace.className,AssemblyName"  table="tableName">


And the hbm-files, I think, should be embedded resources. Check out the Summer of NHibernate Screencast Series, very great video tutorials with provided code!

Hope it helps! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1