gpt4 book ai didi

hibernate - GRAILS GORM保存 child 时如何自动更新父属性

转载 作者:行者123 更新时间:2023-12-02 14:30:00 33 4
gpt4 key购买 nike

我有两个对象。

TimeFrame{

hasMany=[activity:Activity]

Date startTime
Date endTime
Integer activeActivities

}
Activity{
belongsTo=[timeFrame:TimeFrame]

String name
String description
Date start
Date end

}

每当我插入,更新或删除 Activity 时,我都想自动更新时间表的activeActivities数量。但是当我添加GROM事件方法时,例如...
def afterUpdate(){
try{

def num=0;
def now=new Date();
timeFrame.activities.each{i->
if(!i.end || i.end < now){
num+=1;
}
}
timeFrame.activeActivities=num;
timeFrame.save();
}
catch(e){
log.debug('Unable to update active tally on Activity update:'+e)
}
}

我得到了错误
  null id in com.application.tracker.Activity entry (don't flush the Session after an exception occurs).

有什么办法解决这个问题?

最佳答案

saveafterUpdate中失败的原因是因为您没有使用withNewSessiondocumentation指出了这一细节。

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.

关于hibernate - GRAILS GORM保存 child 时如何自动更新父属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37488822/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com