gpt4 book ai didi

NHibernate 查询语法异常

转载 作者:行者123 更新时间:2023-12-03 12:30:34 24 4
gpt4 key购买 nike

我正在关注 Summer of NHibernate Screencast Series并且遇到了一个奇怪的 NHibernate 异常。

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException:
Exception of type
'Antlr.Runtime.NoViableAltException' was thrown.
[select from DataTransfer.Person p where p.FirstName=:fn].

我在以下方面偏离了 Screencast 系列:
  • 针对 MS SQL Server Compact 数据库运行
  • 我正在使用 MSTest 而不是 MbUnit

  • 我尝试了任意数量的查询组合,结果总是相同。我现在的 CreateQuery 语法
    public IList<Person> GetPersonsByFirstName(string firstName)
    {
    ISession session = GetSession();

    return session.CreateQuery("select from Person p " +
    "where p.FirstName=:fn").SetString("fn", firstName)
    .List<Person>();
    }

    虽然不是直接查询,但此方法有效
    public Person GetPersonById(int personId)
    {
    ISession session = GetSession();
    return session.Get<Person>(personId);
    }

    我的 hibernate.cfg.xml
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory name="BookDb">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="connection.connection_string">Data Source=C:\Code\BookCollection\DataAccessLayer\BookCollectionDb.sdf</property>
    <property name="show_sql">true</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <mapping assembly="DataTransfer"/>
    </session-factory>
    </hibernate-configuration>

    人员.hbm.xml
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DataTransfer" namespace="DataTransfer">
    <class name="DataTransfer.Person,DataTransfer" table="Person">
    <id name="PersonId" column="PersonId" type="Int32" unsaved-value="0">
    <generator class="native"/>
    </id>
    <property name="FirstName" column="FirstName" type="String" length="50" not-null="false" />
    <property name="LastName" column="LastName" type="String" length="50" not-null="false" />
    </class>
    </hibernate-mapping>

    最佳答案

    我也在关注 Summer of NHibernate Screencast Series并遇到了同样的问题。

    问题出在 HQL 中“ 从用户 p 中选择” 将其更改为“ 从用户 p 中选择 p | ”或只是“ 来自用户 p ”。

    The ’shorthand’ HQL form that was used in the screencasts under NHibernate version 1.2 was deprecated in 2.0 and eliminated in 2.1.x as the default query parser was switched out to be the more strict option.


    public IList<Person> GetPersonsByFirstName(string firstName)
    {
    ISession session = GetSession();

    return session.CreateQuery("select p from Person p where p.FirstName=:fn")
    .SetString("fn", firstName)
    .List<Person>();
    }

    关于NHibernate 查询语法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1228146/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com