views:

3455

answers:

1

Datastructure is:

  • Table[Questionnaire] the top node
  • Table[QuestionGroup] just a grouping for heading etc
  • Table[QuestionnaireQuestion] mapping [Question]<->[QuestionGroup]s many to many relationship and the [Questionnaire]<->[Question]s many to many relationship
  • Table[Question]

One question can exist in many questionnaires/questiongroups for reporting purposes. The table QuestionnaireQuestion also contains some properties for Question that may differ from questionnaire to questionnaire, like Required? validation etc. What i can't figure out is how to map Question.Required?. The property is unique when filtering the questionnairequestion table on questionnaire_id and question_id

When i load the questionnaire i supply a questionnaire_id which then gives me a List of QuestionGroups each group containing a List of Questions(although missing the validation properties).

So far my mappings are:

   public class QuestionnaireMap : ClassMap<Questionnaire>
    {
        public QuestionnaireMap(){
            WithTable("ida_questionnaire");
            SetAttribute("lazy", "false");
            Id(x => x.ID, "ida_questionnaire_id")
                .WithUnsavedValue(0)
                .GeneratedBy.UuidHex("");

        Map(x => x.Description);

        HasMany<QuestionGroup>(x => x.QuestionGroups)
            .IsInverse()
            .WithKeyColumn("ida_questionnaire_id")
            .AsBag().SetAttribute("cascade", "all");
    }
}

public class QuestionGroupMap : ClassMap<QuestionGroup>
{
    public QuestionGroupMap()
    {
        WithTable("ida_questionnaire_group");
        SetAttribute("lazy", "false");
        Id(x => x.ID, "ida_questionnaire_group_id")
            .WithUnsavedValue(0)
            .GeneratedBy
            .UuidHex("");
        Map(x => x.Description).AsReadOnly();

        //References(x => x.Parent, "ida_questionnaire_id");

        HasManyToMany<Question>(x => x.Questions)
            .WithTableName("ida_questionnaire_question")
            .WithParentKeyColumn("ida_questionnaire_group_id")
            .WithChildKeyColumn("ida_question_id")
            .Cascade.All()
            .IsInverse();
    }
}

public class QuestionMap : ClassMap<Question>
{
    public QuestionMap()
    {
        WithTable("ida_question");
        Id(x => x.ID, "ida_question_id").WithUnsavedValue(0).GeneratedBy.UuidHex("");
        Map(x => x.Description, "description").AsReadOnly();
        Map(x => x.Type, "ida_question_type_id").AsReadOnly();

        Component<Core.Validator>(x => x.MyValidator, m =>
            {
                m.Map(x => x.Type, "ida_question_type_id");
                m.Map(x => x.RangeMin, "validate_min");
                m.Map(x => x.RangeMax, "validate_max");
            });

        HasMany<Alternative>(x => x.Alternatives)
            .IsInverse()
            .WithKeyColumn("ida_question_id")
            .AsBag().SetAttribute("cascade", "all");
    }
}
+1  A: 

You need to map QuestionnaireQuestion as a separate entity. Kyle Bailey gives a good example in his Many-to-many relationships with data attached in NHibernate post.

James Gregory
The link appears to be dead.
ddc0660
http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx this is the correct link to the above article
Khaja Minhajuddin