2

i have spent hours trying to figure this out including going through all the previously asked questions on StackOverflow.

i am trying to query from the TrainingCourse by EvaluationHeadId, thats works fine, however, i try to get the TrainingRoute it returns "could not resolve property: TrainingRoute.TrainingRouteDefinition of: Model.Entities.TrainingCourse"

it saves perfect, my real problem is the query.

updated to:

            using (var session = SessionProvider.Instance.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    dto = session.QueryOver<TrainingCourse>()  
                            .JoinQueryOver<EvaluationHead>(p => p.EvaluationHeads)
                            .JoinQueryOver<TrainingRoute>(p => p.TrainingRoute)
                            .Where(c => c.EvaluationHeadID == headId)
                            .SelectList(l => l
                                .Select(h => h.TrainingCourseDefn).WithAlias(() => d.TrainingCourseDefn)
                                .Select(h => h.IsAvailable).WithAlias(() => d.IsAvailable)
                                .Select(h => h.TrainingRoute.TrainingRouteDefinition).WithAlias(() => d.TrainingRouteDefinition))
                            .TransformUsing(Transformers.AliasToBean<TrainingCourseDTO>())
                            .List<TrainingCourseDTO>();

                    transaction.Commit();           
                }
            }

Mappings:

        public TrainingCourseMap()
    {
        Id(x => x.TrainingCourseID).GeneratedBy.Identity();
        Map(x => x.TrainingCourseDefn);
        Map(x => x.IsAvailable);
        Map(x => x.TrainingCourseCreatedBy);
        Map(x => x.TrainingCourseDtCreation);
        Map(x => x.TrainingCourseDtModified);
        Map(x => x.TrainingCourseModifiedBy);

        References(x => x.TrainingRoute).Column("TrainingRouteID").Cascade.None();
        HasManyToMany(x => x.EvaluationHeads).Table("EvaluationTraining").ParentKeyColumn("TrainingCourseID").ChildKeyColumn("EvaluationHeadID").Inverse().Cascade.All();
    }

        public EvaluationHeadMap()
    {
        Id(x => x.EvaluationHeadID).GeneratedBy.Identity();
        Map(x => x.ManagerID);
        Map(x => x.SupervisorID);
        Map(x => x.EvaluationStartPeriod);
        Map(x => x.EvaluationEndPeriod);
        Map(x => x.EmployeeScalePoint);
        Map(x => x.KRASignature);
        Map(x => x.KRASignatureDate);
        Map(x => x.DateCreated);
        Map(x => x.DateModified);
        HasMany(x => x.KeyResultAreas).KeyColumn("EvaluationHeadID").Cascade.All().Inverse();
        HasMany(x => x.Evaluations).KeyColumn("EvaluationHeadID").Inverse().Cascade.All();

        HasManyToMany(x => x.TrainingCourses).Table("EvaluationTraining").ParentKeyColumn("EvaluationHeadID").ChildKeyColumn("TrainingCourseID").Cascade.All().AsBag();

        References(x => x.Stage).Column("StageID").Cascade.None();
        References(x => x.Employee).Column("EmployeeID").Cascade.None();
        References(x => x.Employment).Column("EmploymentID").Cascade.None();
        //References(x => x.Manager).Column("EmployeeID");
        //References(x => x.Supervisor).Column("EmployeeID");
    }

    public TrainingRouteMap()
    {
        Id(x => x.TrainingRouteID).GeneratedBy.Identity();
        Map(x => x.TrainingRouteDefinition);
        Map(x => x.TrainingRouteDescription);
        HasMany(x => x.TrainingCourses).KeyColumn("TrainingRouteID").Cascade.AllDeleteOrphan().Inverse();
    }

note: i have another query between TrainingCourse and TrainingRoute and it gives no issue at all, even accessing properties through TrainingCourse.TrainingRoute.x pattern. The only difference with this one is that am querying other tables as well.

1
  • sorry I am away at the moment (last minute) and cannot test your solution or provide the mappings. Will try to do that as soon as possible. Kindly bare with me until i do.....i tried to upload it with my question but was told i need more stack points to do so. As u can tell i am pretty new here...including to nhibernate Commented Nov 23, 2013 at 4:24

1 Answer 1

3

The TrainingRoute is a reference property of the TrainingCourse (as well as EvaluationHeads). So you have to use JoinQueryOver or JoinAlias for it as well. Below we will create dummy objects to be used for aliasing (all set to null). We also split the joining of the queries, because they result in a references to newly create queries

TrainingCourse trainingCourse = null;
TrainingRoute trainingRoute = null;
EvaluationHead evaluationHead = null;

var query = session.QueryOver<TrainingCourse>(() => trainingCourse);

// here we can work with criteria against the TrainingRoute
var referenceToTraingRouteQuery = query
    .JoinQueryOver<TrainingRoute>(p => p.TrainingRoute, () => trainingRoute);

// here we can filter the EvaluationHead collection
var referenceToEvaluationHeadQuery = query // here we start again from the base query
    .JoinQueryOver<EvaluationHead>(p => p.EvaluationHeads, () => evaluationHead)
    .Where(c => c.EvaluationHeadID == headId);

dto = query
    .SelectList(l => l
        .Select(() => trainingCourse.TrainingCourseDefn)
                     .WithAlias(() => d.TrainingCourseDefn)
        .Select(() => trainingCourse.IsAvailable)
                     .WithAlias(() => d.IsAvailable)

        // now let's used join alias
        .Select(() => trainingRoute.TrainingRouteDefinition)
                     .WithAlias(() => d.TrainingRouteDefinition))

     .TransformUsing(Transformers.AliasToBean<TrainingCourseDTO>())
     .List<TrainingCourseDTO>();

Or you can use JoinAlias, see more here 16.4. Associations

Sign up to request clarification or add additional context in comments.

7 Comments

sorry I am away at the moment (last minute) and cannot test your solution or provide the mappings. Will try to do that as soon as possible. Kindly bare with me until i do.....i tried to upload it with my question but was told i need more stack points to do so. As u can tell i am pretty new here...including to nhibernate
i have tried another joinQueryOver, however, it puts an error on the line that does the JoinQueryOver<EvaluationHead> :: Delegate 'System.Func<System.Collections.Generic.IEnumerable<Model.Entities.EvaluationHead>>' does not take 1 arguments
could you please udpate the question and show the code? I would like to see the changes you've made. I've tested my suggested scenario and it is working
its a real mystery for me. i have exactly what u suggested.
Sorry, I used wrong scenario locally. I changed the query building to make it more clear. Every JoinQueryOver results in new reference to just created query. So, we will keep the first query as a reference for finall select and transformation. We can also work with each subquery and append some fitlers. Now it should be more close to working state... while I still cannot compare with your reall example
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.