The whole structure is wrong. A one-to-one relationship can't depend on a
criteria query.

Put another way, you must either make it work with just the mapping (I don't
think it's possible), so session.Get<Cat>(id) returns everything correctly,
or do it all in a query, without mapping the relationship.

An alternative is mapping it as it really is in the DB: a List. Then you can
project the properties in the class:

public FavoriteKitten { get { return FavoriteKittens[0]; } }
public SecondFavoriteKitten { get { return FavoriteKittens[1]; } }

    Diego


On Thu, Aug 26, 2010 at 13:28, Mark Wilkins <[email protected]> wrote:

> 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?
>
> Thanks,
>
> Mark
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to