I started reading the "NHibernate Quick Start Guide" from
http://www.hibernate.org/362.html and followed it step by step.
Unfortunately this guide is written for MS SQL Server so I had to adjust
the configuration parameters (in step 4) to those required for MySql
Server. I copied the required parameters from the article "Databases
supported by NHibernate" from http://www.hibernate.org/361.html.
For MySQL one also has to install the MySQL Connector for .NET available
at http://dev.mysql.com/downloads/connector/net/5.0.html
Add a reference to "MySql.Data" to your Visual Studio project and set
the "Copy Local" property to "True". Why that has to be done is
explained in the "NHibernate 1.2 Migration Guide" from
http://www.hibernate.org/407.html#A20
Before I executed the example code I changed the
hibernate.connection.connection_string to match my server configuration:
"Server=vm-mysql50;Database=test;User ID=root;Password=mysql;CharSet=utf8"
Make sure to set the correct CharSet or you'll get an exception later!
When I executed the example code I've run into an exception at
"cfg.AddAssembly(...)":
"NHibernateTests.User.hbm.xml(2,2): XML validation error: Could not find
schema information for the element
'urn:nhibernate-mapping-2.0:hibernate-mapping'."
After some googling I found out that since NHibernate v1.2 the
User.hbm.xml needs a little change: change the
"urn:nhibernate-mapping-2.0" to "urn:nhibernate-mapping-2.2" and the
exception should be gone!
Executing the example code again gave me another exception, this time at
"cfg.BuildSessionFactory()":
"The following types may not be used as proxies:
NHibernateTests.User: method set_Id should be virtual
NHibernateTests.User: method get_Id should be virtual"
mkay.. sounds easy. It wants the properties in the User class to be
virtual, so I made them virtual and the exception was gone.
After these "adjustments" NHibernate finally made a few rows of data
appear in my MySQL database! Have fun..! |