Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NHibernate QuerySyntaxException_Nhibernate_Exception - Fatal编程技术网

NHibernate QuerySyntaxException

NHibernate QuerySyntaxException,nhibernate,exception,Nhibernate,Exception,我跟在后面,遇到了一个奇怪的NHibernate异常 NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. [select from DataTransfer.Person p where p.FirstName=:fn]. 我从以下几个方面偏离了电影放映系列: 在MS SQL Server Compact数据库上运行 我

我跟在后面,遇到了一个奇怪的NHibernate异常

NHibernate.Hql.Ast.ANTLR.QuerySyntaxException:
Exception of type
'Antlr.Runtime.NoViableAltException' was thrown.
[select from DataTransfer.Person p where p.FirstName=:fn].
我从以下几个方面偏离了电影放映系列:

  • 在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 IList GetPersonsByFirstName(字符串名)
    {
    ISession session=GetSession();
    return session.CreateQuery(“从Person p中选择”+
    “其中p.FirstName=:fn”).SetString(“fn”,FirstName)
    .List();
    }
    
    虽然不是直接查询,但此方法有效

    public Person GetPersonById(int personId)
    {
        ISession session = GetSession();
        return session.Get<Person>(personId);
    }
    
    公众人物GetPersonById(int personId)
    {
    ISession session=GetSession();
    返回会话.Get(personId);
    }
    
    我的hibernate.cfg.xml

    
    NHibernate.Connection.DriverConnectionProvider
    NHibernate.Driver.SqlServerCeDriver
    NHibernate.dial.MsSqlCeDialect
    数据源=C:\Code\BookCollection\DataAccessLayer\BookCollectionDb.sdf
    真的
    NHibernate.ByteCode.Castle.proxyFactory,NHibernate.ByteCode.Castle
    
    Person.hbm.xml


    由于您在
    中指定了名称空间,因此我也遵循了,并遇到了相同的问题

    问题在于HQL中的“从用户p选择”将其更改为“从用户p选择p”或仅“从用户p选择p”

    NHibernate版本1.2下的屏幕广播中使用的“速记”HQL表单在2.0中被弃用,在2.1.x中被删除,因为默认查询解析器被切换为更严格的选项

    public IList GetPersonsByFirstName(字符串名)
    {
    ISession session=GetSession();
    return session.CreateQuery(“从Person p中选择p,其中p.FirstName=:fn”)
    .SetString(“fn”,名字)
    .List();
    }
    
    对Person.hbm.xml的更改无效。您指出,反复试验是处理奇怪的NHibernate解析器错误的一种方法,我应该做什么样的试验?将select更改为简单的“select from DATATERVERT.Person p”,并收到相同的错误。我正在使用NHibernate的Build 2.1.0。您正在使用MS SQL Server Compact数据库吗?在我们的工作中,我能看到的唯一的其他区别是,在User.hbm.xml中,您对NHibernateTests程序集的描述更加明确。还有其他想法吗?你有没有试着只选择“来自个人”(这在hql中是合法的)?我实际上是在对sqlite进行测试,但我怀疑这是否重要,因为antlr解析器正在生成查询,如果数据库是问题所在,您将得到一个SQLException,不,显式程序集并没有导致此问题。我不知道您的意思是“来自person p”。这完全奏效了,怎么回事?我想这不是集会,但我想我应该提一下。
    <?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>
    
    <?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>
    
     <class name="Person" table="Person">
      ....
    
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" namespace="NHibernateTests">
    
    <class name="User" table="`User`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="UserId">
      <generator class="assigned" />
    </id>
    <property name="UserName" type="String">
      <column name="UserName" not-null="true" />
    </property>
    <property name="FName" type="String">
      <column name="FName" />
    </property>
      </class></hibernate-mapping>
    
    IList<User> users = session.CreateQuery("select from User p " +
                                  "where p.UserName=:fn").SetString("fn", "u")
                      .List<User>();
    
    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>();
    }