tags:

views:

77

answers:

0

Hey, I need a mapping which maps Point to Question and vice versa (one to one). Point can be created only with association to existing Question, while Question can be stand alone and have Point equal to null. Also it is prefered that there is foreign key in Point table, not in Question table (though I'd be glad if otherwise solution will solve the problem).

Now say we have 3 points and 3 associated quesitons in db. When I query DB with _session.CreateCriteria< Point >().List< Point >() I get the following SQL:

NHibernate: SELECT this_.ID as ID4_1_, this_.Question_id as Question2_4_1_, question2_.ID as ID5_0_ FROM Point this_ inner join Question question2_ on this_.Question_id=question2_.ID

NHibernate: SELECT point0_.ID as ID4_1_, point0_.Question_id as Question2_4_1_, question1_.ID as ID5_0_ FROM Point point0_ inner join Question question1_ on point0_.Question_id=question1_.ID WHERE point0_.Question_id=@p0;@p0 = 1

NHibernate: SELECT point0_.ID as ID4_1_, point0_.Question_id as Question2_4_1_, question1_.ID as ID5_0_ FROM Point point0_ inner join Question question1_ on point0_.Question_id=question1_.ID WHERE point0_.Question_id=@p0;@p0 = 2

NHibernate: SELECT point0_.ID as ID4_1_, point0_.Question_id as Question2_4_1_, question1_.ID as ID5_0_ FROM Point point0_ inner join Question question1_ on point0_.Question_id=question1_.ID WHERE point0_.Question_id=@p0;@p0 = 3

So its 3 more selects than its actually needed though the results returned are correct. What could cause such behaviour and how to fix it?


Db shema looks like this:

http://www.freeimagehosting.net/uploads/9d84a1a8a2.png

Class declarations are:

public class Point
{
    public int ID { get; private set; }

    public Question Question { get; internal set; }
}


public class Question 
{
    public int ID { get; private set; }

    public Point Point {get; set;}
}

Here are mappings using Fluent NHibernate:

public class PointMap : ClassMap<Point>
{
    public PointMap()
    {
        Id(x => x.ID)
            .GeneratedBy.Increment();

        References(x => x.Question)
            .Unique()
            .Not.Nullable();
    }
}

public class QuestionMap : ClassMap<Question>
{
    public QuestionMap()
    {
        Id(x => x.ID)
            .GeneratedBy.Increment();

        HasOne(x => x.Point)
            .PropertyRef(p=>p.Question)
            .Cascade.All();
    }
}