ADO Exception in HQL query
I have 2 classes: Project and DataStructure. Class Project contains member List<DataStructure>. My goal is to load a Project and all its DataStructures in one call.
public class Project
{
public virtual string Id { get { } set { } }
public virtual string Name { get { } set { } }
public virtual ISet<DataStructure> DataStructures { get { } set { } }
}
public class DataStructure
{
public virtual string Id { get { } set { } }
public virtual string Name { get { } set { } }
public virtual string Description { get { } set { } }
public virtual Project Project { get { } set { } }
public virtual IList<DataField> Fields { get { } set { } }
}
Note that DataStructure also contains a list of class DataF开发者_Python百科ield but I don’t want to load these right now.
Mapping in Fluent NHibernate:
public class ProjectMap : ClassMap<Project>
{
public ProjectMap()
{
Table("PROJECTS");
Id(x => x.Pk, "PK");
Map(x => x.Id, "ID");
Map(x => x.Name, "NAME");
HasMany<DataStructure>(x => x.DataStructures).KeyColumn("FK_PROJECT");
}
}
public class DataStructureMap : ClassMap<DataStructure>
{
public DataStructureMap()
{
Table("DATA_STRUCTURES");
Map(x => x.Id, "ID");
Map(x => x.Name, "NAME");
Map(x => x.Description, "DESCRIPTION");
References<Project>(x => x.Project, "FK_PROJECT");
HasMany<DataField>(x => x.Fields).KeyColumn("FK_DATA_STRUCTURE");
}
}
This is my query:
using (ISession session = SessionFactory.OpenSession())
{
IQuery query = session.CreateQuery("from Project pr left join pr.DataStructure");
project = query.List<Project>();
}
query.List() returns this exception:
NHibernate.Exceptions.GenericADOException: Could not execute query[SQL: SQL not available] ---> System.ArgumentException: The value "System.Object[]" is not of type "Project" and cannot be used in this generic collection.
The HQL query you're using selects both Project and DataStructure (just like a regular SQL left join query would). If you want to to query.List<Project>()
you'll have to project the Project from that query: select pr from Project pr left join pr.DataStructure
Is it?
"select pr from Project pr left join pr.DataStructure"
精彩评论