Deal of the Day

Home » Main » Manning Forums » 2007 » NHibernate in Action

Thread: Configuration in NHibernate 2.0 Beta1

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 13 - Pages: 1 - Last Post: Aug 13, 2010 9:34 AM by: Mona2020
wiryadi

Posts: 2
From: Sydney, Australia
Registered: 4/24/05
Configuration in NHibernate 2.0 Beta1
Posted: Jul 13, 2008 10:55 PM
  Click to reply to this thread Reply

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

Posts: 4
From: Norway
Registered: 7/10/08
Re: Configuration in NHibernate 2.0 Beta1
Posted: Jul 14, 2008 3:25 AM   in response to: wiryadi in response to: wiryadi
  Click to reply to this thread Reply

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 :))

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

Posts: 83
Registered: 12/9/05
Re: Configuration in NHibernate 2.0 Beta1
Posted: Jul 14, 2008 6:21 AM   in response to: wiryadi in response to: wiryadi
  Click to reply to this thread Reply

NHibernate.Mapping.Attributes is now in a separated repository called NHContrib.
You can download it here: http://sourceforge.net/projects/nhcontrib/

wiryadi

Posts: 2
From: Sydney, Australia
Registered: 4/24/05
Re: Configuration in NHibernate 2.0 Beta1
Posted: Jul 14, 2008 8:46 PM   in response to: AlexEsser in response to: AlexEsser
  Click to reply to this thread Reply

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

Posts: 38
From: Leeds, UK
Registered: 5/1/08
Re: Configuration in NHibernate 2.0 Beta1
Posted: Aug 1, 2008 6:41 PM   in response to: wiryadi in response to: wiryadi
  Click to reply to this thread Reply

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

Posts: 1
From: England
Registered: 8/28/08
Re: Configuration in NHibernate 2.0 Beta1
Posted: Aug 28, 2008 9:44 AM   in response to: tobinharris in response to: tobinharris
  Click to reply to this thread Reply

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

Posts: 1
From: russia
Registered: 8/30/08
Re: Configuration in NHibernate 2.0 Beta1
Posted: Aug 30, 2008 1:06 PM   in response to: wiryadi in response to: wiryadi
  Click to reply to this thread Reply

> 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

Posts: 4
From: USA
Registered: 1/17/10
Re: Configuration in NHibernate 2.0 Beta1
Posted: Jan 17, 2010 11:54 PM   in response to: wiryadi in response to: wiryadi
  Click to reply to this thread Reply

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. :-)

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("\n{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

Posts: 4
From: USA
Registered: 1/17/10
Re: Configuration in NHibernate 2.0 Beta1
Posted: Jan 18, 2010 10:37 AM   in response to: niberhater in response to: niberhater
  Click to reply to this thread Reply

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

Posts: 83
Registered: 12/9/05
Re: Configuration in NHibernate 2.0 Beta1
Posted: Jan 19, 2010 3:35 AM   in response to: niberhater in response to: niberhater
  Click to reply to this thread Reply

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 :)
Just take the time to learn the meaning of each line of code you see.

HTH,
Pierre Henri.

niberhater

Posts: 4
From: USA
Registered: 1/17/10
Re: Configuration in NHibernate 2.0 Beta1
Posted: Jan 19, 2010 10:46 AM   in response to: kuate in response to: kuate
  Click to reply to this thread Reply

> 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 :)
> 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

Posts: 2
Registered: 8/13/10
Re: Configuration in NHibernate 2.0 Beta1
Posted: Aug 13, 2010 6:53 AM   in response to: niberhater in response to: niberhater
  Click to reply to this thread Reply

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

Posts: 83
Registered: 12/9/05
Re: Configuration in NHibernate 2.0 Beta1
Posted: Aug 13, 2010 8:18 AM   in response to: Mona2020 in response to: Mona2020
  Click to reply to this thread Reply

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

Posts: 2
Registered: 8/13/10
Re: Configuration in NHibernate 2.0 Beta1
Posted: Aug 13, 2010 9:34 AM   in response to: kuate in response to: kuate
  Click to reply to this thread Reply

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.

Legend
Gold: 300 + pts
Silver: 100 - 299 pts
Bronze: 25 - 99 pts
Manning Author
Manning Staff
Manning Developmental Editor