运行 嵌套元素的 hql 查询 returns "unexpected AST node"

Running hql query for nested element returns "unexpected AST node"

当尝试使用通用 executeUpdate(query) 方法更新我的 java 对象时,grails 抛出 NullPointer 异常指出:

Unexpected AST node: .

我的对象关系结构如下:

Class Product implements Serializable {
    String name
    Integer priority
    static belongsTo = [owner: Owner]
    static mapping = {owner fetch: 'join'}
}

Class Owner implements Serializable {
    String name
    Contract contract
    static hasMany = [product: Product]
}

Class Contract implements Serializable {
    Boolean isActive
}

我已成功 运行 我的数据库中的以下 SQL 请求:

UPDATE product SET priority = IF(
    (SELECT co.is_active FROM owner o
    JOIN contract co
    ON co.id = o.contract_id
    WHERE o.id = product.dealership_id) = 1
    , 10, 0);

但是,尝试 运行 grails 中的以下代码抛出 NPE:

def hqlQuery = 'update Product p set p.priority = (case when p.owner.contract.isActive then 10 else 0 end)'
def result = Product.executeUpdate(hqlQuery)

这是为什么?我的 class 映射或 HQL 请求中是否缺少某些内容?

补充说明:

出于好奇,我昨晚设置了一个 sample site,因为它应该可以工作

我认为这可能与事物的定义方式以及您尝试更新的方式有关:

Product: static belongsTo = [owner: Owner]
Owner:  static hasMany = [product: Product]

思考可能是问题的核心,因为您的更新从产品开始或需要更新产品,但当它到达所有者时,它可能有很多该产品。注意到内部连接在查询中出现在本地。

这似乎对我有用:

def hqlQuery = """update Product as p 
                  set p.priority = case when 
                  exists(select 1 from Owner o where o = p.owner and o.contract.isActive is true)
                  then 10
                  else 0
                  end 
                  where id > 0
               """
def result = Product.executeUpdate(hqlQuery)
def found = Product.findAll().priority

Might be related