views:

20

answers:

1

Let's say I have a Cat that has two properties:

  • FavoriteKitten
  • SecondFavoriteKitten

These kittens are discriminated by their Rank.

When loading a Cat, I want the kitten with the rank of "1" to be FavoriteKitten, and the kitten with the rank of "2" to be SecondFavoriteKitten.

The underlying database looks like:

table Cat
----------------
CatId


table Kitten
-----------------
KittenId
CatId
Rank

My mapping looks like:

<class name="Cat">
  ... other stuff
  <one-to-one name="FavoriteKitten" class="Kitten" property-ref="Cat" cascade="all-delete-orphan" />
  <one-to-one name="SecondFavoriteKitten" class="Kitten" property-ref="Cat" cascade="all-delete-orphan" />
</class>

My criteria query looks like

Cat cat = sess.CreateCriteria(typeof(Cat))
.CreateAlias("FavoriteKitten", "kt1")
.Add(Expression.Eq("kt1.Rank", "1"))
.CreateAlias("SecondFavoriteKitten", "kt2")
.Add(Expression.Eq("kt2.Rank", "2"))
.UniqueResult();

The trouble is that once loaded, both FavoriteKitten and SecondFavoriteKitten are the same kitten: the one with a Rank of "2".

Have I left something out of the criteria? Or am I going about this the wrong way?

A: 

Diego, over in the nhibernate mailing list, helped me see the error of my ways. I had everything structured wrong.

Per his suggestion, I decided to map Kittens as it is in the database; as lists.

Mark Wilkins