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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: vs 2005 Setting can seem to make it work
PostPosted: Mon Apr 02, 2007 11:05 am 
Newbie

Joined: Mon Apr 02, 2007 9:56 am
Posts: 11
Hi,
I am new to NHibernate and I am trying to make a simple start up project in Visual Studio 2005. I have looked for answers, and have followed the suggestions in this forum. Yet it has not worked. I think I am really close to get it running. It tells me that nHibernat.User.hbm.xml resource not found. I have included it as an embeded resource. Also I have added to the NHibernate.Configuration. I am sure is something simple.
Thank you in advance.

Hibernate version: 1.2.0

Mapping documents:

file name: user.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="nHibernat.User, App_Code" table="users">

<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>

<!-- user class file from here -->
using System;

namespace nHibernat
{
public class User
{
private string id;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;
...}
}



Code between sessionFactory.openSession() and session.close():

using System;
using System.Web;
using nHibernat;
using NHibernate;
using NHibernate.Cfg;

namespace NHibernate
{
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernat.current_session";
private static readonly ISessionFactory sessionFactory;

static NHibernateHelper()
{
Configuration config = new Configuration();
string sBasePath = System.Web.HttpContext.Current.Server.MapPath(@"~/App_Code/Resources/");
config.AddXmlFile(sBasePath + "user.hbm.xml");
config.AddClass(typeof(nHibernat.User));
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}




Full stack trace of any exception that occurs:

[MappingException: Resource not found: nHibernat.User.hbm.xml]
NHibernate.Cfg.Configuration.LogAndThrow(MappingException me) +38
NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) +133
NHibernate.Cfg.Configuration.AddClass(Type persistentClass) +52
NHibernate.NHibernateHelper..cctor() in C:\Documents and Settings\eherrera\My Documents\Visual Studio 2005\Projects\nHibernat\nHibernat\App_Code\nHibernateHelper.cs:19

[TypeInitializationException: The type initializer for 'NHibernate.NHibernateHelper' threw an exception.]
NHibernate.NHibernateHelper.GetCurrentSession() in C:\Documents and Settings\eherrera\My Documents\Visual Studio 2005\Projects\nHibernat\nHibernat\App_Code\nHibernateHelper.cs:35
nHibernat._Default.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\eherrera\My Documents\Visual Studio 2005\Projects\nHibernat\nHibernat\Default.aspx.cs:23
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061




Name and version of the database you are using: MS SQL Server

The generated SQL (show_sql=true): None

Debug level Hibernate log excerpt:None


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 12:10 pm 
Regular
Regular

Joined: Sun Jan 21, 2007 4:33 pm
Posts: 65
Check your class namespace for User. Right now it's set to 'Nhibernat' and not 'Nhibernate'.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 12:50 pm 
Newbie

Joined: Mon Apr 02, 2007 9:56 am
Posts: 11
I know, I should have made the namespace more clear. The namespace for my class is 'nHibernat' which is different from 'NHibernate' namespace. The classes are set up in the following way:

namespace nHibernat
class user
[b]

[b]
namespace NHibernate
class NHibernateHelper
[b]

Thank you,
Ernesto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 12:58 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
You have nHibernate.User being a part of the 'App_Code' assembly. This assembly doesn't exist. This is all part of the way the WebSite compilation model works...dynamically compiling assemblies and therfore you won't know what the name of the assembly the User class is located in. Drop the 'App_Code' assembly name and just use name="nHibernat.User" .
I'd recommend going to the Web Application Project model to avoid the confusion the WebSite model creates.
Good luck
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 1:31 pm 
Newbie

Joined: Mon Apr 02, 2007 9:56 am
Posts: 11
Hi Mike,

Thank you for tip. That makes a lot of sense. Now, It comes up with an error that says:

[b]"Could not compile the mapping document: c:\....user.hbm.xml"[b]

here is the user.hbm.xml

[b]
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="nHibernat.User, nHibernat.User" table="users">

<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
[b]

Thank you for your help.
Ernesto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 1:38 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
This part of your mapping:
Code:
<class name="nHibernat.User, nHibernat.User" table="users">

is stating that you have an object named User that is in the namespace 'nhibernat' that can be found in the assembly 'nHibernat.User'. Do you have an assembly named 'nHibernat.User'? I recommend changing this to simply
Code:
<class name="nHibernat.User" table="users">

and see what happens.
Actually, I'd recommend that you create a new class library project and placing your domain object into there and get it OUT of the App_code directoy. Then you don't have a moving assembly target. That way you can embed the resource file in the class library project and pass that assembly name to NHibernate to find the mapping correctly.
Placing domain objects in a web project is simply bad practice and will bite you later on.
Hope this helps
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 3:50 pm 
Newbie

Joined: Mon Apr 02, 2007 9:56 am
Posts: 11
Hi Mike,

Thank you for taking a look a this. I created a class library under Space name "QuickStart" and assembly name "clsQuickStart" The user.hbm.xml file is set to compile as resource. All I have in here
is the following:

using System;

namespace QuickStart
{
public class User
{
private string id;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;
...}}
<!-- user.hbm.xml file -->
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="QuickStart.User, clsQuickStart" table="Users">

<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>


I added the class library reference to the web project. Also as before, I have a reference to the NHibernate assembly. Here, I have the following class:

using System;
using System.Web;
using NHibernate;
using NHibernate.Cfg;

namespace NHibernate
{
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernat.current_session";
private static readonly ISessionFactory sessionFactory;

static NHibernateHelper()
{
Configuration config = new Configuration();
config.AddAssembly("clsQuickStart");
sessionFactory = new Configuration().Configure().BuildSessionFactory();

...


Now it is giving me the following error: "problem parsing configuration : System.IO.FileNotFoundException: Could not find file 'C:\\Documents and Settings\.....\nHibernat\\bin\\hibernate.cfg.xml'

From my research, this is where NHibernate is looking for the configuration file. I have this configuration located in the web.config.



<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>

<nhibernate>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI"
/>
</nhibernate>


What am I supposed to do now?

Thank you in advance.
Ernesto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 4:00 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Your call to 'Configure()' is confusing NHibernate...it thinks you have a hibernate.cfg.xml sitting somewhere to look at. I don't usually put my hibernate config in my app.config or my web.config sections. It is usually best to follow the advice in this link on section 3.8, http://www.hibernate.org/hib_docs/nhibe ... ation.html
That way you can move your configuration around as you need and makes your config files more readable.
If you want it in your web.config still, I think you can use this:
Code:
sessionFactory = new Configuration().BuildSessionFactory();

It should detect the <hibernate> section in your web.config file and build it appropriately.

Let me know how that works...
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 8:59 am 
Newbie

Joined: Mon Apr 02, 2007 9:56 am
Posts: 11
Eureka!!!
It worked. Thank you for your guidance. I simply change the call to

Code:
Configuration().BuildSessionFactory()


Now, I am working on the connection to the SQL Server. It is giving me an error that cannot connect to the server. Nonetheless, I am working on it.

Thank you again,
E.


Top
 Profile  
 
 Post subject: resource not found has more than one cause
PostPosted: Tue Apr 03, 2007 10:17 am 
Newbie

Joined: Tue Mar 27, 2007 6:30 am
Posts: 12
I had this problem and all the documentation seems to suggest the embedded resource file.

However, I found there is another cause - not loading the mapping files at runtime.

So it is possible you have set the
<mapping assembly ="assemby name"/>

part of the config wrong.

I'm new to nhibernate, so there may be other causes.

good luck


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 03, 2007 12:06 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
@Ernesto-
I see you have the following in you hibernate config:
Code:
<add
key="hibernate.connection.connection_string"
value="Server=localhost;initial catalog=nhibernate;Integrated Security=SSPI"
/>

Is the name of your database 'nhibernate' ? IF it isn't that is probably helping cause your connection problem. Initial Catalog should be the name of your databse you are connecting to.

Glad you got it going anyways
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 12:01 pm 
Newbie

Joined: Mon Apr 02, 2007 9:56 am
Posts: 11
So I resolved the SQL connection. Then when I run the following code in the code behind page, I get an error

"NHibernate.MappingException: Unknown entity class: QuickStart.User"


Code:
                try
                {
                    ISession session = NHibernateHelper.GetCurrentSession();
                   
                    ITransaction tx = session.BeginTransaction();
                    User newuser = new User();
                    newuser.UserName = "Joseph Cool";
                    newuser.Password = "abc123";
                    newuser.EmailAddress = "joe@cool.com";
                    newuser.LastLogon = DateTime.Now;

                    // Tell NHibernate that this object should be saved
//Here is where the error happens
                    session.Save(newuser);

                    // commit all of the changes to the DB and close the ISession
                    tx.Commit();
             
                    IList userList = session.CreateCriteria(typeof(User)).List();
                    GridView1.DataSource = userList;
                    GridView1.DataBind();
       
                    Label1.Text += userList.Count.ToString();

                    session.Close();
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message;
                }



I read in a blog that this is due to the hbm.xml file not being compiled as a resource. In my case, it is build in the class library as a resource. Here is my resource file:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="QuickStart.User, clsQuickStart" table="Users">
   
    <id name="Id" column="LogonId" type="String" length="20">
      <generator class="assigned" />
    </id>
    <property name="UserName" column="Name" type="String" length="40"/>
    <property name="Password" type="String" length="20"/>
    <property name="EmailAddress" type="String" length="40"/>
    <property name="LastLogon" type="DateTime"/>
  </class>
</hibernate-mapping>


What should I do?
Thank you


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 12:52 pm 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Are you sure you have the file BuildAction as 'Embedded'? Select the file in VS, Go into Properties (F4) and make sure BuildAction is set to 'Embedded' and NHIb should detect it.
MIKE

_________________
If this helped...please remember to rate it!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 1:10 pm 
Newbie

Joined: Mon Apr 02, 2007 9:56 am
Posts: 11
Hi Mike,
Yes, the user.hbm.xml is set to compile as an embedded resource within my class library "QuickStart"
I know the class library is working, but the embedded resource may not.
Thank you,
E.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 05, 2007 1:49 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Your class is not in the QuickStart namespace though. From the code you posted above it looks like the namespace is "nHibernat" (shudder).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.