gpt4 book ai didi

mongodb - 奇怪的行为-mongoDB GORM

转载 作者:行者123 更新时间:2023-12-02 14:52:23 31 4
gpt4 key购买 nike

我有一个使用Grails 2.3.8和Mongo GORM插件3.0.1的Grails应用程序。我有一个服务,该服务在第一次调用时构造一个对象,并将其保存在mongoDB中并返回它。在随后的调用中,它将仅从mongoDB中检索构造的对象并将其返回。

    def loadWeekData(String startDate,String storeId){
def weekJson = WeekJson.findByStoreIdAndStartDate(storeId,startDate)

if(weekJson==null){
//construct weekJson here
weekJson.save(flush:true)
weekJson=WeekJson.findByStoreIdAndStartDate(storeId,startDate)
}


weekJson
}

WeekJson域类还有其他具有hasMany关系的嵌套对象。 WeekJson hasMany Employee hasMany Day hasMany Planned hasMany Activity
WeekJson域类
    public class WeekJson{
static hasMany = [employees:Employee]
static mapWith = "mongo"
static mapping = {
employees fetch: 'join'

}
String toString()
{
"$employees"
}

}

员工域类
    public class Employee {

static mapWith = "mongo"
static hasMany = [days:Day]

static mapping = {
days fetch: 'join'
}
String toString()
{
"$days"
}
}

日域类
    public class Day {

Planned planned;
static mapWith = "mongo"
static constraints = {
planned nullable:true
}

String toString()
{
" plan: $planned "
}

static mapping = { planned lazy:false}
}

计划的域类
     public class Planned {

List<Activity> activities
static hasMany = [activities:Activity]
static mapWith = "mongo"

static mapping = {
activities lazy:false
}

String toString()
{ activities }
}

Activity 域类
    public class Activity {
String inTime;
String outTime;
double duration;
String type;
String desc;
static mapWith = "mongo"
static constraints = {
duration nullable:true
type nullable:true
desc nullable:true
}

String toString()
{
"$inTime to $outTime"
}
}

我已将获取行为更改为渴望在所有具有hasMany关系的类中使用。

第一次,所有嵌套对象都将被正确地构建,并保存在mongoDB中,并且返回的对象是正确的。

但是,对于下一次调用, Activity对象为null。我已验证此调用期间mongoDB中仍然存在嵌套对象。 Planned集合中的记录具有 idActivity集合记录。

当我做,
   println weekJson.employees.days.planned.activities

“ Activity ”列表已打印。然而,
    println weekJson

给出 Activity列表为null,并且渲染为Json。

为什么GORM第二次没有检索嵌套对象?
GORM是否有可能无法处理与此嵌套级别的关系的问题?

最佳答案

也许您应该在域模型中切换到sub-documents

顺便说一句,如果您想帮助我们提供帮助,请在您的案例中发布更多数据:您正在使用哪个版本的mongo,grails等?您的域类是什么样的?保存后您会在mongo集合中看到什么?

关于mongodb - 奇怪的行为-mongoDB GORM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23903613/

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