【问题标题】:insert into .. select in HQL causes MismatchedTreeNodeException插入 .. 在 HQL 中选择会导致 MismatchedTreeNodeException
【发布时间】:2011-01-07 15:12:36
【问题描述】:

我正在尝试使用 HQL 执行看似非常简单的插入 .. 选择,但我被 MismatchedTreeNodeException 难住了。据我所知,我是世界上第一个遇到这种情况的人,我认为这表明我正在做一些非常愚蠢的事情:)。

session.CreateQuery(
    @"insert into PriceValue (Currency, Price, Value)
    select :destinationCurrency, p, pv.Value * :x
    from PriceValue pv
    where pv.Currency = :defaultCurrency" )
    .SetDecimal( "x", x )
    .SetEntity( "destinationCurrency", currency )
    .SetEntity( "defaultCurrency", Config.DefaultCurrency )
    .ExecuteUpdate();

本练习的目标是将新创建的货币的价格值添加到所有现有价格中。

例外是:

QuerySyntaxException:引发了“Antlr.Runtime.MismatchedTreeNodeException”类型的异常。靠近第 1 行,第 63 列 [插入 PriceValue (Currency, AcmeCorp.Core.Models.Products.Price, Value) 选择 :destinationCurrency, pv.Price, pv.Value * :x 来自 AcmeCorp.Core.Models.Products.PriceValue pv 其中 pv.Currency = :defaultCurrency]

一个提示可能是它扩展了部分属性规范 (Price -> AcmeCorp.Core.Models.Products.Price)——异常消息似乎表明 ..Products.Price 附近存在问题。

(修剪后的)映射(由 Castle ActiveRecord 生成)是:

<class name="AcmeCorp.Core.Models.Products.Price, AcmeCorp.Core.Models" table="`price`" schema="`products`">
    <id name="Id" access="property" column="`price_id`" type="Int32" unsaved-value="0">
        <generator class="native"></generator>
    </id>
    <set name="Values" access="nosetter.camelcase" table="`price_value`" lazy="false" inverse="true" cascade="all" fetch="join" batch-size="1000">
        <key column="`price_id`" />
        <one-to-many class="AcmeCorp.Core.Models.Products.PriceValue, AcmeCorp.Core.Models" />
    </set>
</class>

<class name="AcmeCorp.Core.Models.Products.PriceValue, AcmeCorp.Core.Models" table="`price_value`" schema="`products`">
    <id name="Id" access="property" column="`price_value_id`" type="Int32" unsaved-value="0">
        <generator class="native"></generator>
    </id>
    <property name="Value" access="property" type="System.Decimal">
        <column name="`value`"/>
    </property>
    <many-to-one name="Currency" access="property" class="AcmeCorp.Core.Models.Common.Currency, AcmeCorp.Core.Models" column="`currency_code`" unique-key="uk_price_currency" lazy="proxy" />
    <many-to-one name="Price" access="property" class="AcmeCorp.Core.Models.Products.Price, AcmeCorp.Core.Models" column="`price_id`" unique-key="uk_price_currency" lazy="proxy" />
</class>

有什么想法吗?

【问题讨论】:

    标签: nhibernate hql antlr


    【解决方案1】:

    我认为 :x 导致了 MismatchedTreeNodeException,我认为 hql 不会允许选择中的参数。尝试用常量替换它,看看是否有帮助。

    【讨论】:

    • 改为使用 SQL 查询,但将在下周尝试。
    • 遗憾的是,这没有帮助。此外,进一步的实验导致了 EarlyExitException 实例被抛出。我放弃了。
    • 在我的场景中工作过,但 hql 可能是一种黑色艺术
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多