Skip to content

Nhibernate Generating wrong alias with subclass and CrossJoin #2500

@lucasrossoni

Description

@lucasrossoni

I have a project that uses nhibernate version 5.2.7. when i updated him to 5.3.2, i found some problems with queries that involves subclasses;

My domain:

public class TPTAnimal
{
     public virtual int AnimalId { get; set; }
      public virtual string Genus { get; set; }
}
public class TPTMammal : TPTAnimal
 {
       public virtual string MammalName { get; set; }
 }
 public class TPTDog : TPTMammal
{
      public virtual string DogName { get; set; }
}
public class TPTTeste
{
    public virtual int TesteId { get; set; }
    public virtual string TesteName { get; set; }
    public virtual TPTMammal Mammal { get; set; }
}

My Mapping:

public class TPTAnimalMap : ClassMap<TPTAnimal>
    {
        public TPTAnimalMap()
        {
            Table("TPT_ANIMAL");
            Id(x => x.AnimalId).Column("ANIMAL_ID");
            Map(x => x.Genus).Column("GENUS").Length(64);
        }
    }

public class TPTMammalMap : SubclassMap<TPTMammal>
    {
        public TPTMammalMap()
        {
            Table("TPT_MAMMAL");
            KeyColumn("ANIMAL_ID");
            Map(x => x.MammalName).Column("MAMMAL_NAME");
        }
    }

public class TPTDogMap : SubclassMap<TPTDog>
    {
        public TPTDogMap()
        {
            Table("TPT_DOG");
            KeyColumn("ANIMAL_ID");
            Map(x => x.DogName).Column("DOG_NAME");
        }
    }

public class TPTTesteMap : ClassMap<TPTTeste>
    {
        public TPTTesteMap()
        {
            Table("TPT_TESTE");
            Id(x => x.TesteId).Column("TESTE_ID");
            Map(x => x.TesteName).Column("TESTE_NAME");
            References(x => x.Mammal).Column("MAMMAL_ID").Not.Nullable().ForeignKey("FK_05");
        }
    }

So, i wrote this query:

from teste in session.Query<TPTTeste>()
from dog in session.Query<TPTDog>()
where teste.Mammal.AnimalId == dog.AnimalId
select dog

In the version 5.2.7 i got this query, and works fine:

select
	tptdog1_.ANIMAL_ID as animal1_24_,
	tptdog1_2_.GENUS as genus2_24_,
	tptdog1_1_.MAMMAL_NAME as mammal2_25_,
	tptdog1_.DOG_NAME as dog2_26_
from
	TPT_TESTE tptteste0_,
	TPT_DOG tptdog1_
inner join TPT_MAMMAL tptdog1_1_ on
	tptdog1_.ANIMAL_ID = tptdog1_1_.ANIMAL_ID
inner join TPT_ANIMAL tptdog1_2_ on
	tptdog1_.ANIMAL_ID = tptdog1_2_.ANIMAL_ID
where
	tptteste0_.MAMMAL_ID = tptdog1_.ANIMAL_ID
	or (tptteste0_.MAMMAL_ID is null)
	and (tptdog1_.ANIMAL_ID is null)

In the Version 5.3.2 i got this query:

select
	tptdog1_.ANIMAL_ID as animal1_24_,
	tptdog1_2_.GENUS as genus2_24_,
	tptdog1_1_.MAMMAL_NAME as mammal2_25_,
	tptdog1_.DOG_NAME as dog2_26_
from
	TPT_TESTE tptteste0_
cross join TPT_DOG tptdog1_
where
	tptteste0_.MAMMAL_ID = tptdog1_.ANIMAL_ID

I have marked in bold the section that presents the error. The nhibernate created an alias that does not exist.

i don't know if i'm doing something wrong, or the newest version of nhibernate have some problem.

Regardless of how many "levels" of inheritance I have, the problem still happens

There is any way to use inner join intead Cross Join? without override all dialects.

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