Angel \”Java\” Lopez on Blog

April 12, 2011

NHibernate 3 (Part 4) Table Per Class

Filed under: .NET, C Sharp, NHibernate — ajlopez @ 10:50 am

Previous post
Next post

In the previous posts (Table per Hierarchy, Table Per Concrete Class), I implemented two mapping strategies for the same model:

Now, I want to map EACH class to a table:

The solution has two projects, one console project, and the other is a class library:

You can download the code from NHibernate3ItemsTablePerClass.zip. You need to add NHibernate libraries (see a previous post for more details). The database can be created running ExecuteAll.cmd at Sql folder. The command accepts a parameter (e.g. ExecuteAll (local)) as the database server. The default value is .\SQLEXPRESS.

The Item mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ItemsTablePerClass.Domain"
namespace="ItemsTablePerClass.Domain">
  <class name="Item" table="Items">
    <id name="Id">
      <generator class="guid.comb" />
    </id>
    <property name="Title" not-null="true" />
    <property name="Description" not-null="true" />
  </class>
</hibernate-mapping>

There is a table attribute, and no discriminator value. The new “thing” is at Note mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ItemsTablePerClass.Domain"
namespace="ItemsTablePerClass.Domain">
  <joined-subclass name="Note" extends="Item" table="Notes">
    <key column="Id" />
    <property name="Content" />
  </joined-subclass>
</hibernate-mapping>

The new element is joined-subclass. There is a table attribute for this mapping too. And no id element. It was replaces by the key element that joins both tables.

Similar changes at Page mapping:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="ItemsTablePerClass.Domain"
namespace="ItemsTablePerClass.Domain">
  <joined-subclass name="Page" extends="Item" table="Pages">
    <key column="Id" />
    <property name="Url"/>
  </joined-subclass>
</hibernate-mapping>

Notably, the main code is the same as in the previous examples. And the domain classes are still untouched.

Next steps: start to research one-to-many mappings, other alternatives to mapping (Fluent NHibernate, ConfORM, new NHibernate 3.2 API).

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

7 Comments »

  1. […] by NHibernate 3 (Part 4) Table Per Class « Angel “Java” Lopez on Blog — April 12, 2011 @ 10:50 am […]

    Pingback by NHibernate 3 (Part 3) Table Per Concrete Class « Angel “Java” Lopez on Blog — April 12, 2011 @ 1:07 pm

  2. […] Previous post Next post […]

    Pingback by NHibernate 3 (Part 5) First One-To-Many Mapping « Angel “Java” Lopez on Blog — May 9, 2011 @ 9:57 am

  3. Hi my friend! I wish to say that this post is awesome,
    nice written and include almost all significant infos.
    I’d like to see extra posts like this .

    Comment by Diablo 3 key generator — August 30, 2013 @ 5:20 pm

  4. Hello to every , because I am truly eager of reading this weblog’s
    post to be updated regularly. It contains nice data.

    Comment by Noreen — September 10, 2013 @ 8:09 pm

  5. Yesterday, while I was at work, my cousin stole
    my apple ipad and tested to see iif it can survive a thirty foot drop, just
    so she can be a youtube sensation. My apple ipad is now broken and she has 83 views.
    I know this is totally ooff topic but I had to share it wiuth someone!

    Comment by Webcams Porn Videos — September 10, 2013 @ 8:52 pm

  6. Hello, this weekend is pleasant in favor oof me, since
    this point in time i aam reading this great
    educational pist here at my house.

    Comment by Whooty Can Shake It — September 12, 2013 @ 8:35 am

  7. Thank you for another informative web site.
    Where else may I am getting that kind of information
    written in such an idal way? I’ve a project
    that I’mjust now operating on, and I have been at the look out for such info.

    Comment by Carley — September 12, 2013 @ 8:32 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: