简体   繁体   English

nHibernate SQL不可用错误

[英]nHibernate SQL Not Available error

I have the following code to execute a simple select statement. 我有以下代码来执行简单的select语句。 This query fails given the "SQL Not Available" error. 鉴于出现“ SQL Not Available”错误,该查询失败。 This same code as a pattern works just fine in about 20 different models. 与模式相同的代码在大约20种不同的模型中都可以正常工作。 In fact if I alter the design of the view implemented in SQL to say hide the LoadID column the query fails stating "Can execute ... ... because LoadID doesn't exist. 实际上,如果我更改了用SQL实现的视图的设计以说隐藏LoadID列,则查询将失败,并说明“可以执行... ...,因为LoadID不存在。

My problem is when this code, the hbm.xml model, and the design of the view are all aligned the ses.Query() fails stating "SQL not available" 我的问题是,当这段代码,hbm.xml模型和视图的设计都与ses.Query()对齐时,声明“ SQL不可用”失败

var rows = ses.Query<vInventory>().Where(c => c.CustomerRecid == recid).Select(c => new vInventory()
{
    InventoryRecid = c.InventoryRecid
    , ItemID = c.ItemID
    , LoadID = c.LoadID
    , CustomerRecid = c.CustomerRecid
}).ToList();

This is the mapping. 这是映射。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="Asmbly" namespace="Asmbly.Models">
  <class name ="vInventory" table="dbo.vInventory" dynamic-update="true">
    <cache usage="read-only"/>
    <id name="InventoryRecid" column="InventoryRecid" type="Int64">
      <generator class="native" />
    </id>
    <property name="ItemID" />
    <property name="LoadID" />
    <property name="CustomerRecid" />
  </class>
</hibernate-mapping>

Update. 更新。 I took the output of the SQL view and dumped it into a table. 我获取了SQL视图的输出并将其转储到表中。 Then modified the hbm.xlm to point to the table. 然后修改hbm.xlm以指向该表。 Now the ses.Query() works fine. 现在,ses.Query()可以正常工作。 This is NOT a workable possibility. 这不是可行的可能性。

Please comment on InventoryRecid. 请在InventoryRecid上发表评论。 In the SQL view this attribute is 在SQL视图中,此属性为

ROW_NUMBER() over (...)

How can I get nHibernate to use this view and utilize the InventoryRecid attribute? 如何使nHibernate使用此视图并利用InventoryRecid属性? I will never have attempt an update to this entity. 我将永远不会尝试对此实体进行更新。

Update #2: I changed the hbm.xlm mapping from 更新#2:我从更改了hbm.xlm映射

<class name ="vInventory" table="dbo.vInventory" dynamic-update="true">

to

<class name ="vInventory" table="dbo.vInventory" dynamic-update="false">

recompiled and my problem is gone. 重新编译,我的问题就解决了。

Unfortunately, I change it back to true and recompile and my problem doesn't come back. 不幸的是,我将其改回true并重新编译,但问题没有回来。

Is there a special reason you are creating new instances of vInventory in the Select method? Select方法中创建vInventory新实例是否有特殊原因? nHibernate should already return objects of this type based on your mapping. nHibernate应该已经根据您的映射返回了这种类型的对象。 I think it may screw up the linq provider as a constructor call can't be translated to SQL. 我认为这可能会破坏linq提供程序,因为构造函数调用无法转换为SQL。 The linq provider will handle the Select statement as if it were a normal IEnumerable . linq提供程序将像处理普通IEnumerable一样处理Select语句。

Now what happens is: nHibernate gets a query where only this: .Where(c => c.CustomerRecid == recid) needs to get executed. 现在发生的是: .Where(c => c.CustomerRecid == recid)仅在以下位置获取查询: .Where(c => c.CustomerRecid == recid)需要执行。 So nHibernates only queries for the IDs of the objects (because as far as nHibernate knows, it doesn't need any more properties). 因此,nHibernate仅查询对象的ID(因为据nHibernate所知,它不再需要其他属性)。 Which will give you 1, 2, 3, 4, 5, ... and so on. 这将为您提供1, 2, 3, 4, 5, ...等。

As soon as your Select gets executed (not on the database) nHibernate figures it needs the rest of the properties so it queries for them one by one by their ID (not very efficient with many results). 一旦Select执行(不在数据库上),nHibernate就会确定需要其余的属性,因此它会按ID逐一查询它们(效率不高,结果很多)。 Now your using a ROW_NUMBER as an ID wchich can be dangerous because it only takes the current resultset into account. 现在,使用ROW_NUMBER作为ID密码可能会很危险,因为它仅考虑了当前结果集。 When querying for a single record the ROW_NUMBER will always be 1, so nHibernate can't find the rest of your objects anymore. 查询单个记录时, ROW_NUMBER始终为1,因此nHibernate不再能够找到其余的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM