These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Exception: Mapping exception.
PostPosted: Mon Sep 17, 2007 5:12 am 
Newbie

Joined: Mon Sep 17, 2007 5:01 am
Posts: 4
Hi everyone, I've got a problem when I tried to map the hbm.xml file with the .cs file. When I launch the program I've got a MappingException:

Resource not found: Cat.hbm.xml

I can't figure out why it doesn't work with my code.
If someone can help me, I'll be grateful :D

Thanks.

Hibernate version: 1.2.0

Files:

App.conf

Code:
<?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="dialect">
        NHibernate.Dialect.MsSql2005Dialect
      </property>
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="connection.connection_string">
        Server=(localhost);initial catalog=Quickstart;Integrated Security=SSI
      </property>
     
      <mapping assembly="Quickstart" />
      <mapping resource="Cat.hbm.xml" assembly="Quickstart" />
    </session-factory>
  </hibernate-configuration>

</configuration>


Cat.hbm.xml

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Quickstart" assembly="Quickstart">
 
  <class name="Cat" table="Cat">

    <id name="Id" column="CatId" type="Char" length="32">
      <generator class="assigned" />
    </id>

    <property name="Name" column="Name" type="String" length="40" not-null="true" />
    <property name="Sex" column="Sex" type="Char" length="1" not-null="false"/>

  </class>
 
</hibernate-mapping>


Cat.cs

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace Quickstart
{
    public class Cat
    {
        private string id;
        private string name;
        private char sex;

        public Cat()
        {
        }
        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public char Sex
        {
            get { return sex; }
            set { sex = value; }
        }
    }
}


Program.cs
Code:
Configuration cfg = new Configuration();
cfg.Configure(); // <-- Crash here
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
...


Full stack trace of any exception that occurs:

" at NHibernate.Cfg.Configuration.LogAndThrow(MappingException me)\r\n at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)\r\n at NHibernate.Cfg.Configuration.DoConfigure(XmlDocument doc)\r\n at NHibernate.Cfg.Configuration.Configure(XmlTextReader reader)\r\n at NHibernate.Cfg.Configuration.Configure(XmlNode node)\r\n at NHibernate.Cfg.Configuration.Configure()\r\n at Quickstart.Program.Main(String[] args) in C:\\Documents and Settings\\dde\\My Documents\\Visual Studio 2005\\Projects\\Quickstart\\Quickstart\\Program.cs:line 18\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()"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 9:28 am 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Do you have Cat.hbm.xml set as an embedded resource (in file's properties)? You need it to be embedded so that NHibernate can retrieve it from the assembly manifest.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 9:29 am 
Newbie

Joined: Mon Sep 17, 2007 5:01 am
Posts: 4
Yes, I've added Cat.hbm.xml as an embedded ressource :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 1:23 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
In your NHibernate config, you need to make sure that you specify your mapping assemblies:

Code:
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
...
      [b]<mapping assembly="My.Assembly.Name" />[/b]
    </session-factory>
  </hibernate-configuration>


Otherwise you would have to specify the assembly programatically in your code.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 1:47 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
MEvo, if those suggestions don't work, two other thoughts:

First, if you are using Visual Studio to build, and you added Cat.hbm.xml as a resource after your initial build, then building again will do nothing--VS doesn't include resource changes when considering whether a project has "changed" or not. You need to go to the Build menu and Rebuild the solution, which forces recompilation.

Second, check for the resource in the assembly manifest using ildasm.exe. Just because the file name is Cat.hbm.xml, doesn't mean that is the resource name. It may have prepended the directory or something, like Quickstart.Cat.hbm.xml.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 2:20 pm 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
marcal wrote:
First, if you are using Visual Studio to build, and you added Cat.hbm.xml as a resource after your initial build, then building again will do nothing--VS doesn't include resource changes when considering whether a project has "changed" or not. You need to go to the Build menu and Rebuild the solution, which forces recompilation.

Totally true, in fact this can be troublesome at any time when you after mapping files only and then build. I tend to rebuild as a habit when building my assemblies with mapping files embedded within.

Now, by bad. I get the bad code reader award of the day. Reading through the config, I see this:
Quote:
<mapping assembly="Quickstart" />
<mapping resource="Cat.hbm.xml" assembly="Quickstart" />

You shouldn't need to specify all your mapping files in your config, but if you did the names would have to be fully qualified. I'd first try removing the resource mapping and leave <mapping assembly="Quickstart" />. If you did use <mapping resource="Cat.hbm.xml" assembly="Quickstart" /> for each of you mapped classes I don't think you'd need <mapping assembly="Quickstart" />.

Let me know how it all works out.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 2:53 am 
Newbie

Joined: Mon Sep 17, 2007 5:01 am
Posts: 4
I rebuild my solution and as you said in my app.config I removed the other mappings and now I've only the <mapping assembly="Quickstart" /> mapping. I thought I tried this solution, it seems not because now it works :D

Thanks to you all.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 18, 2007 6:42 am 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Great! And don't forget to rate the postings that help you (not just mine).

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.