mongodb - Grails MongoDB插件如何处理多态?

标签 mongodb grails gorm polymorphic-associations

我有一个类似以下的对象:

class User {
    static mapWith="mongo"
    static embedded = [ 'profiles' ]

    String email
    List<Profile> profiles;
}

interface Profile {
}

class Profile1 implements Profile {
}

class Profile2 implements Profile {
}

如果我将具体的类Profile1或Profile2添加到User对象并将其保存到数据库,则在从MongoDB中读回该对象时会引发异常。在这种情况下,我看不到任何信息会保存到数据库中以标识应实例化的对象类型。确切地,有关于该案件如何处理的零文档。其他框架具有处理此问题的机制,因此Grails MongoDB可能严重损坏,或者再次没有记录。那么我该如何解决呢?

异常(exception)情况如下:
| Error 2013-06-12 18:48:00,390 [http-bio-8080-exec-5] ERROR errors.GrailsExceptionResolver  - InstantiationException occurred when processing request: [POST] /mpa/user/authenticate -parameters:

  email: carhubb@gmail.com
  password: ***
  com.mycompany.security.Profile. Stacktrace follows:
  Message: com.mycompany.security.Profile
  Line | Method
  ->>  342 | newInstance0                        in java.lang.Class
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  |    310 | newInstance                         in     ''

最佳答案

Grails MongoDB完全没有损坏,每个用例都可能没有记录在案。 :)
您的用例可以按预期工作,并且如下所示已成功测试。

// src/groovy
interface Profile {
    Integer getDuration()
}

import org.bson.types.ObjectId
class Profile1 implements Profile {
    ObjectId id
    String profileName
    String type
    Date effectiveStartDate
    Date effectiveEndDate

    Integer getDuration(){
        effectiveEndDate - effectiveStartDate
    }

    static mapWith = "mongo"
}

import org.bson.types.ObjectId
class Profile2 implements Profile{
    ObjectId id
    String profileName
    String type
    Date effectiveStartDate
    Date effectiveEndDate

    static mapWith = "mongo"

    Integer getDuration(){
        effectiveEndDate - effectiveStartDate
    }
}
class User {
    ObjectId id

    static mapWith = "mongo"
    static embedded = ['profiles']

    String email
    List<Profile> profiles
}

class UserController {

    def index() {
        def profile1 = new Profile1(type: 'Individual',
                                    profileName: 'IndividualProfile',
                                    effectiveStartDate: new Date(),
                                    effectiveEndDate: new Date() + 100) as Profile
        def profile2 = new Profile2(type: 'Company',
                                    profileName: 'CompanyProfile',
                                    effectiveStartDate: new Date(),
                                    effectiveEndDate: new Date() + 50) as Profile

        println profile1.duration //prints 100
        println profile2.duration //prints 50

        def user = new User(profiles: [profile1, profile2], email: 'abc@gmail.com').save(flush: true)

        render user as JSON
    }
}

//db.user.find()
{ 
    "_id" : ObjectId("51ba8d55892cb98368b2f1e5"), 
    "email" : "abc@gmail.com", 
    "profiles" : [{   
            "effectiveEndDate" : ISODate("2013-09-22T03:26:13.396Z"),   
            "effectiveStartDate" : ISODate("2013-06-14T03:26:13.396Z"),   
            "profileName" : "IndividualProfile",    
            "type" : "Individual" 
        },
        {   
            "effectiveEndDate" : ISODate("2013-08-03T03:26:13.423Z"),       
            "effectiveStartDate" : ISODate("2013-06-14T03:26:13.423Z"),   
            "profileName" : "CompanyProfile",
            "type" : "Company" 
        } 
    ], 
    "version" : 0 
}

您还可以找到上面的设置here

注意:-为了简化用法,Profile1Profile2的设计相似。

关于mongodb - Grails MongoDB插件如何处理多态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17093076/

相关文章:

grails - 如何在Grails迁移中将不可为空的列更改为可为空?

list - 基于另一个列表的 Groovy 排序列表

Grails war 没有 java 源

grails - 在GORM中对可为空的字段进行排序

grails - 如何在jar包中激活域类?

mongodb - 如何表示具有混合类型的数组

java - Java Spring 中是否可以进行条件聚合?

javascript - 绑定(bind) observableArray 中未定义的字段或提供默认值

python - 将 Unicode 转换为等效 ASCII (SCRAPY)

grails - 我可以在grails的同一字段中使用多个ilike吗?