NHibernate 查询异常

发布于 2024-10-09 23:57:58 字数 2792 浏览 1 评论 0原文

我在执行最基本的查询时遇到映射异常。 这是我的域类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在风中等你 2024-10-16 23:57:58

Yoav 的问题是您需要指定映射文件所在的程序集,而不是实体所在的位置。将呼叫更改为

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProjectMap>())

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

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProjectMap>())
Smile简单爱 2024-10-16 23:57:58

首先确保您的映射类确实被执行。您可以通过在 ProjectMap 类中放置断点然后进行调试以确保其执行来实现这一点。

using (ISession Session = SessionFactory.OpenSession())
{
    DetachedCriteria Filter = DetachedCriteria.For<Project>();
    var Projects = Filter.GetExecutableCriteria(Session).List<Project>();
}

尝试使用 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.

using (ISession Session = SessionFactory.OpenSession())
{
    DetachedCriteria Filter = DetachedCriteria.For<Project>();
    var Projects = Filter.GetExecutableCriteria(Session).List<Project>();
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文