SexyMF
SexyMF

Reputation: 11155

Fluent Nhibernate - make References from children side only

I have Survey that HasMany Question that HasMany Option.
I want to create a reference from Option to Survey.
The problem is that Every time I want to create a new Survey with all of its children, I need to loop on all survey.Questions and loop on all survey.Questions.Options and set o each Option the Survey (or else Nhibernate will create a new Survey for each Option)
** I dont want to have IList<Option> on Survey if I do that it will solve the problem but it would be heavy and unusable **

public class SurveyMap : ClassMap<Survey>
    {
        public SurveyMap()
        {
           Id(x => x.Id).GeneratedBy.Identity();
            HasMany(x => x.Questions).KeyColumn("SurveyId").Inverse().AsBag().Not.LazyLoad().Cascade.SaveUpdate();
        }
    }

    public class QuestionMap : ClassMap<Question>
    {
        public QuestionMap()
        {
           Id(x => x.Id).GeneratedBy.Identity();
            HasMany(x => x.Options).KeyColumn("QuestionId").Inverse().AsBag().Cascade.SaveUpdate();
        }
    }

    public class OptionMap : ClassMap<Option>
    {
        public OptionMap()
        {
            Id(x => x.Id).GeneratedBy.Identity();
            References(x => x.Survey).Column("SurveyId").Cascade.All();
        }
    }

Upvotes: 1

Views: 320

Answers (1)

forseta
forseta

Reputation: 89

It might be better to separate your reporting concerns from your applications business logic concerns. Use the mappings that you already have for your applications business logic, and provide a different interface into the data for your reporting requirements. You could use either HQL, SQL or new objects and maps for this.

Upvotes: 1

Related Questions