2016-04-05 30 views
0

我们允许数据库中有重复记录,但id不同。更新记录时,我们希望找到并更新任何重复项。通过研究,我发现了在update和afterUpdate之前的Hibernate事件方法。问题是,当我找到副本,并开始通过他们迭代,我得到这个错误:如何更新grails中的重复记录

错误:行被其它事务更新或删除(或者未保存值的映射是不正确的)

这是我更新后代码域:

1  def afterUpdate() { 
2   println "\n*** afterUpdate ***" 
3   def record = this 
4 
5   DomainObject.createCriteria().list() { 
6    ne('id', record.id) 
7    or { 
8     eq('storeId', record.storeId) 
9     and { 
10      eq('firstName', record.firstName) 
11      eq('lastName', record.lastName) 
12      eq('dateOfBirth', record.dateOfBirth) 
13     } 
14    } 
15    //eq('lastUpdated', record.lastUpdated) 
16   }.each { dupe -> 
17    log.info "syncing ${dupe.id} with ${record.id}" 
18    dupe.properties.each{ key, value -> 
19     println "*** prop: ${key}: ${value}" 
20    } 
21   } 
22  } 
23 

的错误是在19行,我们正在使用的Grails 2.4.4和开发在Windows

回答

2

看一看的documentation。注意本节有关使用withNewSession

class Person { 
    String name 
    def beforeDelete() { 
     ActivityTrace.withNewSession { 
     new ActivityTrace(eventName: "Person Deleted", data: name).save() 
     } 
    } 
} 

Notice the usage of withNewSession method above. Since events are triggered whilst Hibernate is flushing using persistence methods like save() and delete() won't result in objects being saved unless you run your operations with a new Session.

Fortunately the withNewSession method lets you share the same transactional JDBC connection even though you're using a different underlying Session.

+0

废话,我必须错过了.withNewSession一部分!谢谢约书亚! – havoc74

相关问题