wiryadi (2) [Avatar] Offline
#1
Hi,

I was following up the example in chapter 2 (Hello NHibernate!)

1. I could not find NHibernate.Mapping.Attributes.dll anymore in NHibernate 2.0 Beta 1

2. When I ran the code I got the following error:
{"Could not compile the mapping document: HelloNHibernate.Employee.hbm.xml"}
The inner exception was:
{"Could not find the dialect in the configuration"}

Thank you

Wiryadi
AlexEsser (5) [Avatar] Offline
#2
Re: Configuration in NHibernate 2.0 Beta1
I ran into quite the same problem. The following solved it for me:
1. Downloaded Beta2 (don't know if that i necessary, but can't hurt smilie)

2. Changed the 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">
Data Source=127.0.0.1;Integrated Security=True; initial catalog=NHibernateSample
</property>
</session-factory>
</hibernate-configuration>
</configuration>

After that it worked for me. But remember to change the connection.connection_string

Hope that helps
Alex
kuate (83) [Avatar] Offline
#3
Re: Configuration in NHibernate 2.0 Beta1
NHibernate.Mapping.Attributes is now in a separated repository called NHContrib.
You can download it here: http://sourceforge.net/projects/nhcontrib/
wiryadi (2) [Avatar] Offline
#4
Re: Configuration in NHibernate 2.0 Beta1
Thanks Alex,

I tried your suggestion on Beta 1, it worked.

Should the book section be revised to reflect future NHibernate release?

Cheers,

Wiryadi
tobinharris (38) [Avatar] Offline
#5
Re: Configuration in NHibernate 2.0 Beta1
The book is written to the current stable version of NHibernate (1.2.1), but it might be worth mentioning yes. I'll run this past Kuate.

Thanks

Tobin
bixbarton (1) [Avatar] Offline
#6
Re: Configuration in NHibernate 2.0 Beta1
Tobin, it might be a good idea, as it appears that NHibernate is now only available as 2.0GA. So 1.2.1 is no longer the stable release.
nurka (1) [Avatar] Offline
#7
Re: Configuration in NHibernate 2.0 Beta1
> Hi,
>
> I was following up the example in chapter 2 (Hello
> NHibernate!)
>
> 1. I could not find NHibernate.Mapping.Attributes.dll
> anymore in NHibernate 2.0 Beta 1
>
> 2. When I ran the code I got the following error:
> {"Could not compile the mapping document:
> HelloNHibernate.Employee.hbm.xml"}
> The inner exception was:
> {"Could not find the dialect in the configuration"}
>
> Thank you
>
> Wiryadi
niberhater (4) [Avatar] Offline
#8
Re: Configuration in NHibernate 2.0 Beta1
Please let me reactivate this thread because I am having precisely the same problem:

"Could not find the dialect in the configuration".

So, I put my details here, hoping to get my problem resolved. Any hint or suggestion is highly appreciated. Please let me succeed with Nhibernate at least once. smilie

I've been struggling with this problem for hours and cannot google out a solution. Well actually, to be accurate, I have been trying to use NHibernate for the past few months, but I have never had luck. The problem is, either I haven't found a good tutorial, or there just isn't any on the web. The one at http://nhforge.org/wikis/howtonh/your-first-nhibernate-based-application.aspx doesn't really help a beginner. You can tell from the reader comments there. I followed that example, and didn't get it to work. My problem? Maybe, but quite a few tutorials even have very bad mistakes that smell a irresponsible copy-paste. For example, I remember two articles mess up Users and Products, in other words, they have User table, but in their code, they are talking about Product. Hilarious.

So, I bought NHibernate in Action from Amazon, and followed the HelloNHibernate example in the 2nd chapter almost word by word, letter by letter. But in the end, it still does not work. I am having very bad NHibernate experience so far.

Below, I paste the project file code, just to make the situation clear.

//My Employee table in my HelloNHibernate database in a Sql Server 2008 instance:

CREATE Table Employee
(
Id INT identity primary key,
Name varchar(50),
Manager INT
)
Go



//My Employee.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true">
<class name="HelloNHibernate.Employee, HelloNHibernate" lazy="false">
<id name="Id" access="field">
<generator class="native" />
</id>
<property name="Name" access="field" column="Name" />
<many-to-one access="field" name="Manager" column="Manager" cascade="all" />
</class>
</hibernate-mapping>

//My hibernate.cfg.xml, the book actually doesn't say we need hibernate.cfg.xml,
//but if I name the file App.config like what the book says,
// an exception is thrown in OpenSession() at
// var c = new Configuration();

<?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="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Data Source=MySqlServer2008_Instance; Initial Catalog=HelloNHibernate; Integrated Security=SSPI;
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2005Dialect
</property>
<property name="show_sql">
false
</property>
</session-factory>
</hibernate-configuration>
<nhibernate>
</nhibernate>
</configuration>

//My Employee.cs (I am using the automatic property feature):

namespace HelloNHibernate {
class Employee {
public int Id { get; set; }
public string Name { get; set; }
public Employee Manager { get; set; }

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

//My Program.cs (followed the code in NHibernate in Action),
//method OpenSession throws an exception at
//c.AddAssembly(Assembly.GetCallingAssembly());
// The exception is: {"Could not find the dialect in the configuration"}

using NHibernate;
using NHibernate.Cfg;
using System.Reflection;
using System;

namespace HelloNHibernate {
class Program {
static void Main(string[] args) {
CreateEmployeeAndSaveToDatabase();
UpdateTobinAndAssignPierreHenriAsManager();
LoadEmployeeFromDatabase();
Console.WriteLine("Press any key to exit ...");
Console.ReadKey();
}

static void CreateEmployeeAndSaveToDatabase()
{
var tobin = new Employee();
tobin.Name = "Tobin Harris";
using (var session = OpenSession())
{
using (var transaction = session.BeginTransaction())
{
session.Save(tobin);
transaction.Commit();
}
Console.WriteLine("Saved Tobin to the database");
}
}

static ISession OpenSession()
{
if (factory == null)
{
try
{
var c = new Configuration();

// The following line throws an Exception, which says
// {"Could not compile the mapping document: HelloNHibernate.Employee.hbm.xml"}
// {"Could not find the dialect in the configuration"}

c.AddAssembly(Assembly.GetCallingAssembly());
factory = c.BuildSessionFactory();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

return factory.OpenSession();
}

static void LoadEmployeeFromDatabase()
{
using (var session = OpenSession())
{
var query = session.CreateQuery("from Employee as emp order by emp.Name asc");
var foundEmployees = query.List<Employee>();

Console.WriteLine("
{0} employees found:", foundEmployees.Count);

foreach(var e in foundEmployees)
{
Console.WriteLine(e.SayHello());
}
}
}

static void UpdateTobinAndAssignPierreHenriAsManager()
{
using (var session = OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var query = session.CreateQuery("from Employee where name='Tobin Harris'");
var tobin = query.List<Employee>()[0];
tobin.Name = "Tobin David Harris";
var pierreHenri = new Employee();
pierreHenri.Name = "Pierre Henri Kuate";
tobin.Manager = pierreHenri;
transaction.Commit();
Console.WriteLine("Updated Tobin and added Pierre Henri");
}
}
}

private static ISessionFactory factory;
}
}
niberhater (4) [Avatar] Offline
#9
Re: Configuration in NHibernate 2.0 Beta1
I downloaded the source code of the book from http://www.manning.com/kuate/ and played with it. It works if I keep the Id, Name and Manager fields public like this:

public int Id;
public string Name;
public Employee Manager;

But, as soon as I change these 3 lines to

public int Id { get; set; }
public string Name { get; set; }
public Employee Manager { get; set; }

the line:

c.AddAssembly(Assembly.GetCallingAssembly());

throws an exception which says:

{"Could not compile the mapping document: HelloNHibernate.HelloNHibernate.Employee.hbm.xml"}

{"Problem trying to set property type by reflection"}

I changed them to automatic properties, because in a real project, we would be doing this instead of making them all public.

What's going on? Thanks.
kuate (83) [Avatar] Offline
#10
Re: Configuration in NHibernate 2.0 Beta1
Hi,

You must also remove all the access="field" from the mapping XML file.

NHibernate is a quite complex tool; but I'm sure that the worse is now behind you smilie
Just take the time to learn the meaning of each line of code you see.

HTH,
Pierre Henri.
niberhater (4) [Avatar] Offline
#11
Re: Configuration in NHibernate 2.0 Beta1
> Hi,
>
> You must also remove all the access="field"
> from the mapping XML file.
>
> NHibernate is a quite complex tool; but I'm sure that
> the worse is now behind you smilie
> Just take the time to learn the meaning of each line
> of code you see.
>
> HTH,
> Pierre Henri.


Thank you very much for this precious info. That solved the problem. The information you provided here is very likely the only piece in the Internet universe as well as physical books that talks about how to configure NHibernate for auto properties. If not, please give me a reference where this has been addressed or discussed. I am worried about the code base quality of NHibernate. We quite often run into performance issues using NHibernate.
Mona2020 (2) [Avatar] Offline
#12
Re: Configuration in NHibernate 2.0 Beta1
Hi
I tried to write the HelloNHibernate following step by step according the book but I got this error : "Could not compile the mapping document: HelloNHibernate._2._0.HelloNHibernate.Employee.hbm.xml"

then I downloaded the sourcecode from "http://www.manning.com/kuate/" it works with no problem with my data base and insert and update my database. I made a new project and copied all the codes exactly in my new project I got again the same error I really cant understand the difference between the original project and my new made project, they are completely the same!!

Can anyone tell me why it has error when I implemente exactly the same codes
kuate (83) [Avatar] Offline
#13
Re: Configuration in NHibernate 2.0 Beta1
Hi,

You probably have the same problem as:
http://www.manning-sandbox.com/thread.jspa?threadID=38654

Check the inner exception (ie: read the whole exception text); it should give you a clue as to what the exact problem is.

HTH,
Pierre Henri.
Mona2020 (2) [Avatar] Offline
#14
Re: Configuration in NHibernate 2.0 Beta1
Thx,

the InnerException was "Persistent class HelloHibernate.Employee, HelloHibernate not found" whatever I did in my Employee class it encountered error again. I made new project and copy/paste ready classes and xml files in it, it works. I hope not to have problem in my real project which I am going to start.