-->
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: Mapping problem
PostPosted: Wed Jul 15, 2009 1:23 am 
Newbie

Joined: Tue Jul 14, 2009 8:49 am
Posts: 5
I am trying to follow an NHibernate 1.2 HelloWorld application on VS2008/SQL 2005 RTM/NHibernate 2.1.x. I get a MappingException was unhandled error saying "No persister for:HelloWorldNHibernate.Employee". Googling suggested two probable causes for this problem. One, my Build Action for the hbm mapping file is not set to Embedded Resource (this I have set). Second, there's a syntax problem in the mapping file. But I can't figure out any problems in the mapping file as such. Any help is appreciated!

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="HelloWorldNHibernate"
assembly="HelloWorldNHibernate">
<class name="HelloWorldNHibernate.Employee" table="Employee">
<id name="ID" column="ID">
<generator class="identity"/>
</id>
<property name="Name" column="Name"/>
<many-to-one access="field" name="Manager" column="Manager" cascade="all"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Mapping problem
PostPosted: Wed Jul 15, 2009 2:48 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You probably have a problem with your hibernate configuration. There've been breaking changes between 1.2 and 2.0:

•<nhibernate> section is ignored, using <hibernate-configuration> section (note that they have different XML formats)
•Configuration values are no longer prefixed by "hibernate.", if before you would specify "hibernate.dialect", now you specify just "dialect"
•Will perform validation on all named queries at initialization time, and throw exception if any is not valid.

Can you post your configuration ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Mapping problem
PostPosted: Wed Jul 15, 2009 6:10 am 
Newbie

Joined: Tue Jul 14, 2009 8:49 am
Posts: 5
Right.. I managed to get rid of the previous error by changing the way the mapping file was written. Now I get a "Could not compile the mapping document:HelloWorldNHibernate.Employee.hbm.xml" error. The inner exception says {"class HelloWorldNHibernate.Employee, HelloWorldNHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null not found while looking for property: Manager"}

Employee.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="HelloWorldNHibernate"
assembly="HelloWorldNHibernate">
<class name="HelloWorldNHibernate.Employee, HelloWorldNHibernate" table="Employee">
<id name="ID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="Name" column="Name" type="String" length="50"/>
<many-to-one access="field" name="Manager" column="Manager" cascade="all"/>
</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<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=localhost;Initial Catalog=HelloNHibernate;Integrated Security=SSPI</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: Mapping problem
PostPosted: Wed Jul 15, 2009 6:12 am 
Newbie

Joined: Tue Jul 14, 2009 8:49 am
Posts: 5
This is the Employee class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorldNHibernate
{
public class Employee
{
private int _id;
private string _name;
private Employee _manager;

public int ID
{
get { return _id;}
set { _id=value;}
}

public string Name
{
get { return _name; }
set { _name = value; }
}

public Employee Manager
{
get { return _manager;}
set { _manager = value; }
}

public string SayHello()
{
return string.Format("Hello World!', said {0}", Name);
}
}
}

Calling Program:

using System;
using System.Collections.Generic;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;

namespace HelloWorldNHibernate
{
class Program
{
static void Main(string[] args)
{
CreateEmployeeAndSaveToDatabase();
LoadEmployeeFromDatabase();

Console.WriteLine("Press <Enter> to exit");
Console.ReadLine();
}

static void CreateEmployeeAndSaveToDatabase()
{
Employee emp = new Employee();
emp.Name = "Sreedhar";

using (ISession session = OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(emp);
transaction.Commit();
}
Console.WriteLine("Saved Employee to database");
}
}

static ISession OpenSession()
{
Configuration c = new Configuration();
//c.AddAssembly(Assembly.GetCallingAssembly());
c.Configure();
c.AddAssembly(typeof(Employee).Assembly);
ISessionFactory f = c.BuildSessionFactory();
return f.OpenSession();
}

static void LoadEmployeeFromDatabase()
{
using (ISession nhibernateSession = OpenSession())
{
IQuery query = nhibernateSession.CreateQuery("from Employee as emp order by emp.Name");
IList<Employee> foundEmployee = query.List<Employee>();
Console.WriteLine("\n{0} employees found", foundEmployee.Count);
foreach (Employee emp in foundEmployee)
{
Console.WriteLine(emp.SayHello());
}
}
}
}
}


Top
 Profile  
 
 Post subject: Re: Mapping problem
PostPosted: Wed Jul 15, 2009 6:30 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
<many-to-one access="field" name="Manager" column="Manager" cascade="all"/>

You're using field-based access, but your naming is wrong. You either have to change the field to

private Employee manager;

or change tha mapping to

field="field.camelcase-underscore"

Have a look at the tables at the end of that chapter:

http://nhforge.org/doc/nh/en/index.html#mapping-declaration-property

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Mapping problem
PostPosted: Wed Jul 15, 2009 7:28 am 
Newbie

Joined: Tue Jul 14, 2009 8:49 am
Posts: 5
Thanx wolli this indeed was the problem! I'd just copied the original code, without realising. Thanks for that link. I removed the access attribute so it could default to property! I can see the SQL being generated. However, now I am getting an error since my ID field is not null in the database. The SQL is as follows:

NHibernate: INSERT INTO Employee (Name,Manager) VALUES (@p0, @p1); select SCOPE_IDENTITY;@p0 = 'Sreedhar', @p1 = NULL

What happened to my ID field? Why is it not getting picked up in the SQL?


Top
 Profile  
 
 Post subject: Re: Mapping problem
PostPosted: Wed Jul 15, 2009 7:36 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You're using generator "native" which means the id will be generated in the database. Therefore no id is in the insert. You have to set the column to identity.

id name="ID" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Mapping problem
PostPosted: Wed Jul 15, 2009 7:50 am 
Newbie

Joined: Tue Jul 14, 2009 8:49 am
Posts: 5
Brilliant :) !! I converted <generator class = "assigned"/> and assigned a value to the id in the calling program! Sweet success .. my first NHibernate now works like a charm. Thanks a ton wolli for all the assistance. Next step is to Update and then finally Delete the entry .. this is cool stuff!


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.