Skip to content

NH-3359 - QueryOver polymorphic entity results in unwanted inner join #1294

Open
@nhibernate-bot

Description

@nhibernate-bot

C J created an issue — 12th December 2012, 14:59:52:

Dear NHibernate-Team,

I'm facing the following problem using the query over syntax:

If two different subclasses contain a property (1:n) with an identical signature the following QueryOver
(interested in the first subclass property) will also inner join the not to be queried second subclass property.
Please check the following example for explanation:

The QueryOver:


TheResult resultAlias = null;
TheSpecificJoin specificJoinAlias = null;
var query = QueryOver	.Of<TheTableToQuery>()
						.Where(t => t.SomeProperty.Id == searchedId)
						.JoinAlias(t => t.TheBaseClassJoin, () => specificJoinAlias)
						.JoinAlias(t => specificJoinAlias.TheProblematicProperty, () => problematicPropertyAlias)
						.Select(t => problematicPropertyAlias.Id);

The Maps:

  • TheBaseClassJoin-Map:

    public class TheBaseClassJoinMap : ClassMap<TheBaseClassJoin>
    {
        public TheBaseClassJoinMap()
        {
            Table("the*base_class*join");

            Id(x => x.Id).GeneratedBy.Sequence("seq*base*class");
		}
	}

  • TheSpecificJoin-Map:

	public class TheSpecificJoinMap 
        : SubclassMap<TheSpecificJoin>
    {
        public TheSpecificJoinMap()
        {
			Table("the*specific*join");
            KeyColumn("id");
			
            HasManyToMany(x => x.TheProblematicProperty)
              .AsBag()
              .Table("problematic*to_be_joined_one_to*n")
              .ParentKeyColumn("specific*join*id")
              .ChildKeyColumn("target_id")
              .OrderBy("target_id");
		}
	}

  • TheSpecificClassNotToBeInnerJoined-Map:

	public class TheSpecificClassNotToBeInnerJoinedMap 
        : SubclassMap<TheSpecificClassNotToBeInnerJoined>
    {
        public TheSpecificClassNotToBeInnerJoinedMap()
        {
			Table("the*specific_class_not_to_be_inner*joined");
            KeyColumn("id");
			
            HasManyToMany(x => x.TheProblematicProperty)
              .AsBag()
              .Table("problematic*not_to_be_inner_joined_one_to*n")
              .ParentKeyColumn("specific*class_not_to_be_inner_joined*id")
              .ChildKeyColumn("target_id")
              .OrderBy("target_id");
		}
	}

The mapped Classes:

  • TheBaseClassJoin:

	public abstract class TheBaseClassJoin
    {
		...
        
		protected internal virtual long Id { get; set; }
		
		...
	}

  • TheSpecificJoin:

	public abstract class TheSpecificJoin : TheBaseClassJoin
    {
		...
        
        public virtual IEnumerable<Target> TheProblematicProperty { get; protected set; }
		
		...
	}

  • TheSpecificClassNotToBeInnerJoined:


	public abstract class TheSpecificClassNotToBeInnerJoined : TheBaseClassJoin
    {
		...
        
        public virtual IEnumerable<Target> TheProblematicProperty { get; protected set; }
		
		...
	}

The resulting Oracle-Sql:


SELECT target1*.target_id AS y0*
FROM   the*table_to_query this_0*

INNER JOIN the*base_class_join baseclass1*
ON this*0_.baseclass_id = baseclass1*.id

LEFT OUTER JOIN the*specific_join instructio1_1*
ON baseclass1*.id = baseclass1_1*.id

LEFT OUTER JOIN the*specific_class_not_to_be_inner_joined baseclass1_2*
ON baseclass1*.id = baseclass1_2*.id

INNER JOIN problematic*to_be_joined_one_to_n problematicproperty1*
ON baseclass1*.id = problematicproperty1_.specific_join*id
INNER JOIN target target1_
ON problematicproperty1*.target_id = target1*.id

-- START: should not be present

INNER JOIN problematic*not_to_be_inner_joined_one_to_n problematicproperty2*
ON baseclass1*.id = problematicproperty2_.specific_class_not_to_be_inner_joined*id
INNER JOIN target target1_
ON problematicproperty2*.target_id = target1*.id

-- END: should not be present

WHERE  this*0_.some_property*id = :p0

Any help would be appreciated,

Greetz CJ

Version: 3.3.0.4000

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions