2016-04-05 32 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

相關問題