NHibernate 查询异常
我在执行最基本的查询时遇到映射异常。 这是我的域类:
public class Project
{
public virtual string PK { get; set; }
public virtual string Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
和映射类:
public class ProjectMap :ClassMap<Project>
{
public ProjectMap()
{
Table("PROJECTS");
Id(x => x.PK, "PK");
Map(x => x.Id, "ID");
Map(x => x.Name, "NAME");
Map(x => x.Description, "DESCRIPTION");
}
}
配置:
public ISessionFactory SessionFactory
{
return Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(c => c.Is("data source=" + path)))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Project>())
.BuildSessionFactory();
}
和查询: IList项目;
using (ISession session = SessionFactory.OpenSession())
{
IQuery query = session.CreateQuery("from Project");
project = query.List<Project>();
}
我在查询行上遇到异常:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Project is not mapped [from Project]
at NHibernate.Hql.Ast.ANTLR.SessionFactoryHelperExtensions.RequireClassPersister(String name)
at NHibernate.Hql.Ast.ANTLR.Tree.FromElementFactory.AddFromElement()
at NHibernate.Hql.Ast.ANTLR.Tree.FromClause.AddFromElement(String path, IASTNode alias)
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElementList()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromClause()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.unionedQuery()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.query()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectStatement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.statement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(HqlParseEngine parser, String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary`2 replacements, Boolean shallow)
at NHibernate.Engine.Query.HQLQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
at NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)
我认为我的查询有问题。
I'm getting a mapping exception doing the most basic query.
This is my domain class:
public class Project
{
public virtual string PK { get; set; }
public virtual string Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
And the mapping class:
public class ProjectMap :ClassMap<Project>
{
public ProjectMap()
{
Table("PROJECTS");
Id(x => x.PK, "PK");
Map(x => x.Id, "ID");
Map(x => x.Name, "NAME");
Map(x => x.Description, "DESCRIPTION");
}
}
Configuration:
public ISessionFactory SessionFactory
{
return Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard.ShowSql().ConnectionString(c => c.Is("data source=" + path)))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Project>())
.BuildSessionFactory();
}
And query:
IList project;
using (ISession session = SessionFactory.OpenSession())
{
IQuery query = session.CreateQuery("from Project");
project = query.List<Project>();
}
I'm getting the exception on the query line:
NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Project is not mapped [from Project]
at NHibernate.Hql.Ast.ANTLR.SessionFactoryHelperExtensions.RequireClassPersister(String name)
at NHibernate.Hql.Ast.ANTLR.Tree.FromElementFactory.AddFromElement()
at NHibernate.Hql.Ast.ANTLR.Tree.FromClause.AddFromElement(String path, IASTNode alias)
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromElementList()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.fromClause()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.unionedQuery()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.query()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.selectStatement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlWalker.statement()
at NHibernate.Hql.Ast.ANTLR.HqlSqlTranslator.Translate()
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Analyze(HqlParseEngine parser, String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.DoCompile(IDictionary`2 replacements, Boolean shallow, String collectionRole)
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.Compile(IDictionary`2 replacements, Boolean shallow)
at NHibernate.Engine.Query.HQLQueryPlan..ctor(String hql, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.QueryPlanCache.GetHQLQueryPlan(String queryString, Boolean shallow, IDictionary`2 enabledFilters)
at NHibernate.Impl.AbstractSessionImpl.GetHQLQueryPlan(String query, Boolean shallow)
at NHibernate.Impl.AbstractSessionImpl.CreateQuery(String queryString)
I assume something is wrong with my query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Yoav 的问题是您需要指定映射文件所在的程序集,而不是实体所在的位置。将呼叫更改为
Yoav the problem is that you need to specify the assembly where your mapping files are, not where your Entities are. Change the call to
首先确保您的映射类确实被执行。您可以通过在 ProjectMap 类中放置断点然后进行调试以确保其执行来实现这一点。
尝试使用 DetachedCriteria 来确保您的查询不会拼写错误...我通常使用 DetachedCriteria 进行查询,因为这样我就不需要知道 Hql。
First make sure that your mapping class is actually executed. You can do that by placing break points inside the ProjectMap class and then debugging to ensure it is executed.
Try using DetachedCriteria to ensure your query is not miss spelled ... I usually use the DetachedCriteria for querying because then I don't need to know the Hql.