0

nHibernate is throwing an ADOException when I try to Query using this:

session.Query<Owner>()
    .Where(x => x.Animal.Environments.Any(e => e == "Garage")
        && x.Animal.Name == "Lassie" && x.OwnerName == "John Doe");

The generated SQL shows that it's using an alias that does not exist.

Here's the mapping and entities:

    public class Owner
    {
        public Owner()
        {

        }

        public Owner(IAnimalBase animal)
        {
            this.Animal = animal;
        }

        public virtual IAnimalBase Animal { get; protected set; }

        public virtual string OwnerName { get; set; }

        public virtual Guid Id { get; set; }
    }

    public class Dog : AnimalBase
    {
        public virtual string BarkType { get; set; }
    }

    public interface IAnimalBase
    {
        string Name { get; set; }

        IList<string> Environments { get; set; }
        Guid Id { get; set; }
    }

    public abstract class AnimalBase : IAnimalBase
    {
        public virtual Guid Id { get; set; }

        public virtual string Name { get; set; }

        public virtual IList<string> Environments { get; set; }
    }

public class DogMap : SubclassMap<Dog>
{
    public DogMap()
    {
        this.Map(x => x.BarkType);

        this.DiscriminatorValue("D");
    }
}

    public OwnerMap()
    {
        this.Id(x => x.Id).GeneratedBy.GuidComb();

        this.Map(x => x.OwnerName);

        this.References(x => x.Animal).Class<AnimalBase>().Not.LazyLoad().Cascade.All();
    }

    public AnimalBaseMap()
    {
        this.Id(x => x.Id).GeneratedBy.GuidComb();

        this.HasMany(x => x.Environments).Table("AnimalEnvironments").Element("Environment").Cascade.All();

        this.Map(x => x.Name);

        this.DiscriminateSubClassesOnColumn("Aid");
    }

It is complaining about animalbase1_.Id.

select owner0_.Id        as Id2_,
       owner0_.OwnerName as OwnerName2_,
       owner0_.Animal_id as Animal3_2_
from   "Owner" owner0_,
       "AnimalBase" animalbase3_
where  owner0_.Animal_id = animalbase3_.Id
       and (exists (select environmen2_.Environment
                    from   AnimalEnvironments environmen2_
                    where  owner0_.Animal_id = animalbase1_.Id
                           and animalbase1_.Id = environmen2_.AnimalBase_id
                           and environmen2_.Environment = 'Garage' /* @p0 */))
       and animalbase3_.Name = 'Lassie' /* @p1 */
       and owner0_.OwnerName = 'John Doe' /* @p2 */

Thank you!

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
Nventex
  • 1
  • 2

1 Answers1

0

You may want to map to the actual class instead of the interface. I'm sure there are valid reasons to use the interface I've never had to...

Ref: Fluent NHibernate, working with interfaces

Untested just a basic example

public class Owner
 {
    public Owner()
     {
         Dog = new List<Dog>();
     }
     public virtual IList<Dog> Dog { get; set; }
     public virtual string OwnerName { get; set; }
     public virtual Guid Id { get; set; }
 }

 public class Dog 
 {
     public Dog()
     {
         AnimalBase = new AnimalBase();
     }
     public virtual string BarkType { get; set; }
     public virtual AnimalBase AnimalBase { get; set; }
 }

 public class AnimalBase 
 {
     public AnimalBase()
     {
         Environments = new List<strings>();
     }
     public virtual string Name { get; set; }
     public virtual IList<string> Environments { get; set; }
 }
Community
  • 1
  • 1
WeisserHund
  • 150
  • 10