NHibernate. (Another Guide). For Visual Studio 2005.

March 13, 20085 Comments
Facebook Twitter Pinterest Plusone

NHibernate..Oh how I have struggled with you for the past day and a half or so..But eventually got there. Here’s what to do with Visual Studio 2005 that they dont tell you anywhere else.

In your bin folder…I have a hibernate.cfg.xml file.. Which looks like this badboy.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<hibernate-configuration xmlns=”urn:nhibernate-configuration-2.2″>
<session-factory>
<property name=”connection.connection_string”>Data Source=localhost\SQLEXPRESS;Initial Catalog=ncatalog;Trusted_Connection=Yes;</property>
<property name=”connection.provider”>NHibernate.Connection.DriverConnectionProvider</property>
<property name=”dialect”>NHibernate.Dialect.MsSql2000Dialect</property>
<property name=”connection.driver_class”>NHibernate.Driver.SqlClientDriver</property>
<property name=”cache.use_query_cache”>false</property>
<property name=”connection.isolation”>ReadCommitted</property>
<property name=”default_schema”>dbo</property>
<property name=”show_sql”>true</property>
</session-factory>
</hibernate-configuration>

I also have my hbm.xml file in there…which looks like this, notice the URN:hibernate-mapping-2.2. If you get invalid schema errors, its prob because the example in the quickstart uses 2.0. Also notice the properties of “namespace” and “assembly”. These aren’t in the freakin example either. Why the example doesn’t use an identity column I have no idea, it must be the most common of table structures.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<hibernate-mapping xmlns=”urn:nhibernate-mapping-2.2″ namespace=”NewsmanagerDLL” assembly=”NewsmanagerDLL”>
<class name=”NewsmanagerDLL.Authors” table=”tbl_authors”>
<id name=”id” column=”fld_author_id” type=”Int32″>
<generator class=”identity” />
</id>
<property name=”authorname” column=”fld_author_name” type=”String” length=”40″/>
<property name=”isauthoradmin” column=”fld_author_admin” type=”Boolean” />
<property name=”authorusername” column=”fld_author_user” type=”String” length=”40″/>
<property name=”authoremail” column=”fld_author_email” type=”String” length=”40″/>
<property name=”authorpassword” column=”fld_author_pass” type=”String” length=”40″/>
<property name=”authornotify” column=”fld_author_notify” type=”Boolean”/>
</class>
</hibernate-mapping>

Put your business logic layers in a separate dll, i.e. a new project. Compile the dll, add it as a reference in your NHibernate project. Oh did I mention the error…

Could not find a getter for property ‘Id’ in class

means that the xml file is being read…but the class properties might be uppercase. change them all to lower case. You can figure out my properties from my xml file.

nhibernate.gifThen use AddFile instead of AddAssembly..I couldn’t find the “embed resources property” in visual studio 2005. See screenshot to the right..clarification on this welcome.

Dim cfg As Configuration = New Configuration
cfg.Configure()
cfg.AddFile(Server.MapPath(“Bin/NewsmanagerDLL.Authors.hbm.xml”))
Dim factory As ISessionFactory = cfg.BuildSessionFactory
Dim session As ISession
session = factory.OpenSession
Dim transaction As ITransaction
transaction = session.BeginTransaction
Dim objAuthor As New Authors
objAuthor.AuthorEmail = “paul@webdistortion.com”
objAuthor.AuthorName = “NHiver”
objAuthor.AuthorNotify = True
objAuthor.AuthorPassword = “Nhibernate”
objAuthor.IsAuthorAdmin = True
session.Save(objAuthor)
transaction.Commit()
session.Close()

Leave me a comment if this cleared anything up for you. Oh – and take a gander at some other programming related posts.

Filed in: ProgrammingWeb Design
Tagged with:

About the Author ()

Paul is a regular 30 year old web bloke / programmer with a penchant for online marketing. This blog is a personal outlet, with an eclectic mix of articles.

Comments (5)

Trackback URL | Comments RSS Feed

  1. The embed ressource is not available in the web project solutions as it is filesystem based. Instead use a class library or if you still want it in a web project use a web application project

  2. Þór says:

    Hi,

    How did you solve that NHibernate doesn’t show up in the .NET references in “Add reference” (VS2005, web/C# project).

  3. admin says:

    Erm…just browse for the dll?

  4. santhiya says:

    The description given for nhibernate is nice and

    simple.In the above example you had given the explanation

    only for insert option.But how the nhibernate works for

    Update,Delete and Select options explanations can also be

    included.

  5. sjors miltenburg says:

    thanks a lot for the uppercase hint, it was just what i was looking for!

Leave a Reply

Back to Top