School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,125 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,041 people online right now. Registration is fast and FREE... Join Now!




nHibernate and mapping files.

 

nHibernate and mapping files., Error: "Could not compile the mapping document"

Methical

14 May, 2009 - 12:00 AM
Post #1

New D.I.C Head
*

Joined: 8 May, 2009
Posts: 8

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.
IPB Image

Now for the dirty bits;

App.Config
xml

<?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

<?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.
IPB 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
c#

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
CODE
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...
CODE

"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.
c#

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.








User is offlineProfile CardPM
+Quote Post


Wimpy

RE: NHibernate And Mapping Files.

24 Jun, 2009 - 08:24 AM
Post #2

I feel happy... *singing*
Group Icon

Joined: 2 May, 2009
Posts: 958



Thanked: 132 times
Dream Kudos: 75
My Contributions
The <class> property in the hbm-file should be formatted like this:
CODE
<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! smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:17PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month