I am using NHibernate 3.2.0 with an Microsoft SQL Server 2005 database, C# 4.0 and the following domain:
public class Foo
{
public virtual Guid Id { get; set; }
public virtual IBar MyBar { get; set; }
}
public interface IBar
{
Guid Id { get; set; }
}
public class CocktailBar : IBar
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
public class ProgressBar : IBar
{
public virtual Guid Id { get; set; }
public virtual int Progress { get; set; }
}
and the mapping
<hibernate-mapping>
<class name="Foo" table="FOO">
<id name="Id">
<generator class="guid.comb"/>
</id>
<any name="MyBar" meta-type="System.Guid" id-type="System.Guid">
<meta-value class="MyNamespace.CocktailBar" value="716B5C04-4115-47BF-BE8A-A3B34D3607FC"/>
<meta-value class="MyNamespace.ProgressBar" value="65412938-C2DE-48FF-9E90-009881DBDD4F"/>
<column name="TypeOfBarId"/>
<column name="BarObjectId"/>
</any>
</class>
</hibernate-mapping>
Now I'm trying to create a query that returns all Foo objects that are associated with a CocktailBar that has the name "Nightbar". How do I create this query? Using implicit polymorphism in queries (be it HQL, Criteria or QueryOver) is not covered in the NHibernate Reference documentation.
I tried the following:
Foo theFoo = session.QueryOver<Foo>()
.Where(c => c.MyBar.GetType().Name == "CocktailBar")
.JoinQueryOver<IBar>(c => c.MyBar)
.Where(c => ((CocktailBar)c).Name == "Nightbar")
.SingleOrDefault<Foo>();
And got the exception: "System.InvalidOperationException : any types do not have a unique referenced persister" in NHibernate.Type.AnyType.GetAssociatedEntityName