使用grails域基于多列更新表
我试图在where子句中使用grails域的多个列的基础上更新表中的一列 我的域类如下所示: 类Objectattributestrans{ 字符串id 字符串属性值//50 //字符串transid//50 同上使用grails域基于多列更新表,grails,Grails,我试图在where子句中使用grails域的多个列的基础上更新表中的一列 我的域类如下所示: 类Objectattributestrans{ 字符串id 字符串属性值//50 //字符串transid//50 同上 static mapping = { id column: 'trans_id' //['transid', 'attribid'] table name: 'object_attributes_trans' att
static mapping = {
id column: 'trans_id' //['transid', 'attribid']
table name: 'object_attributes_trans'
attrvalue column: 'value'
attribid column:'oa_attrib_id'
version false
}
static constraints = {
attribid blank:false, nullable:false
id blank:false, nullable:false
attrvalue maxSize: 50, nullable:true
}
}
我正在使用此域进行更新,如下所示:
Objectattributestrans.findAllByIdAndAttribid(transacId, queryKey).each {
it.attrvalue = updateVal.toString()
it.save(flush:true) ; // this will perform "update"
}
但是,无论是什么样的查询,都将在下面给出
更新对象属性转换集oa属性id=,值=?其中trans_id=
但是,我希望update语句
更新对象属性转换集值=?其中trans_id=?和oa_attrib_id=
如何使用上面的域来执行此操作
谢谢。您的代码示例有点不清楚。我不太清楚你是否有复合主键 除此之外,当使用动态查找器findAllBy时。。。正如您在示例中看到的,GORM/Hibernate将检索所有匹配对象的集合。如果您随后遍历它们,更新每个对象并调用save,GORM/Hibernate将为每个对象发出UPDATE语句。UPDATE语句将仅引用主键/id字段,而不是原始条件 这样会更有效率,更接近你的目标?将where查询与updateAll或类似的HQL语句一起使用 1使用where查询 2 Hibernate HQL方法注意,对于HQL不支持的非RDBMS数据存储,这将不起作用
非常感谢安德鲁…:-
def query = Objectattributestrans.where {
transid == myTransid && attribid == myAttribid
}
int total = query.updateAll(attrvalue: updateVal)
Objectattributestrans.executeUpdate(
"update Objectattributestrans o set o.attrvalue = :newAttrvalue " +
"where o.transid = :transid and o.attribid = :attribid",
[newAttrvalue: updateVal, transid: myTransid, attribid: myAttribid])