[英]grails create criteria non unique object error if wrapped in withNewSession
I have criteria that looks like this 我有看起来像这样的标准
//Inventory.withNewSession{
Set inventory=Inventory.withCriteria{
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
inventoryDetail{
eq('userAdded',true)
}
...
}
//}
If I wrap that in a withNewSession I get duplicate key and non unique object in the session errors. 如果将其包装在withNewSession中,则会在会话错误中得到重复的键和非唯一对象。 If I don't use withNewSession, it works. 如果我不使用withNewSession,它会起作用。
Why? 为什么?
If you already had a session and loaded object with id=1
(eg), and then created a new session and loaded that same object. 如果您已经有一个会话并加载了id=1
(例如)的对象,然后创建了一个新会话并加载了该对象。 When the new session block ends, the hibernate session joins the existing session (am I saying this right?) and now you have your both objects loaded with the same ID. 当新的会话块结束时,休眠会话将加入现有的会话(我是说对吗?),现在,您的两个对象都加载了相同的ID。
I'm not sure why you're using withNewSession
here, is there a path that this may execute without a hibernate session? 我不确定您为什么在这里使用withNewSession
,是否存在无需休眠会话即可执行的路径? Are you trying, as TDC suggests, to isolate this transactionally? 您是否按照贸发会议的建议,尝试以事务隔离的方式进行隔离?
maybe try to use replace withNewSession with withTransaction? 也许尝试与withTransaction一起使用replace withNewSession?
Inventory.withTransaction{
Set inventory=Inventory.withCriteria{
setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
inventoryDetail{
eq('userAdded',true)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.