[英]Grails search in hibernate session
When using the save()
without flush: true
, the instance is in the hibernate session, not in the database. 使用不带
flush: true
的save()
,实例在休眠会话中,而不在数据库中。 So is there anyway that we can search the instances in the session? 那么无论如何,我们可以在会话中搜索实例吗?
Update: I have a queue of objects and use them to either create or update another object Number: 更新:我有一个对象队列,并使用它们来创建或更新另一个对象。
def inst = getNextOneFromQueue()
Number num = Number.findWhere(a:inst.a, b:ins.b)
If (num == null){
num = new Number()
}
num.a = inst.a
num.b = inst.b
num.save()
ins.delete(flush:true)
So the problem is i have to set flush:true in the delete or save method for each object I have in the queue. 所以问题是我必须在队列中的每个对象的delete或save方法中设置flush:true。 Otherwise if object num is created but not flushed to the database, there might be a possibility that an update on num could not find it in the database and create a new one (which is a duplicate).
否则,如果创建了对象num但未将其刷新到数据库,则可能存在对num的更新无法在数据库中找到它并创建新对象(重复)的可能性。
But flushing every time is really inefficient. 但是每次冲洗确实效率很低。 I'm thinking about not setting flush:true and search in the hibernate session to do the update.
我正在考虑不设置flush:true并在休眠会话中搜索以进行更新。 Is this possible?
这可能吗? Any Suggestions?
有什么建议么? Work around?
变通吗?
I did a test. 我做了一个测试。 Create a new Number object num, save it without flush and then search for it right away:
创建一个新的Number对象num,将其保存而不刷新,然后立即进行搜索:
Number num = new Number()
num.a = 234
num.save()
def temp = Number.findWhere(a:234) //Number.findByA(234)
temp is NULL. temp为NULL。 Apparently findWhere/findByA is search in the database.
显然,findWhere / findByA是在数据库中搜索。
Retry your example, set failOnError: true
to see if there is any error while save()
重试您的示例,设置
failOnError: true
以查看save()
时是否有任何错误
OR Use this example instead to get a valid json response. 或使用此示例获取有效的json响应。
//Controller action
def index() {
new Number(a: 10, b: 25).save()
render Number.findWhere(a: 10) as JSON
}
//Number.groovy
class Number {
Integer a
Integer b
}
Grails 2.2.2 Grails 2.2.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.