当尝试使用通用executeUpdate(query)
方法更新我的java对象时,grails会抛出一个NullPointer异常,说明:
意外的AST节点:。
我的对象关系结构如下:
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)
这是为什么 ?我的类映射或HQL请求中是否缺少某些内容?
附加说明:
p.owner.contract.isActive
没有问题出于好奇,我昨晚深夜设置了一个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