3

I have two domain classes

one is User.groovy which is storing in mongodb

class User {

    String firstName
    String lastName
    Address address

    static mapWith = "mongo"
    static mapping = {version false

    }

    static constraints = {
        address nullable: true
    }
}

second one is Address which is storing mysqlDB

class Address {

    String address1

    String address2

    String city

    String state

    String country


    static constraints = {
    }
}

when i am running below logic

def userInstance=User.get(1l)
println "--->>"+userInstance?.address?.address1

Error loading association [1] of type [class com.imomentous.Address]. Associated instance no longer exists.

when i am running below logic

def userInstance=User.get(1l)
println "--->>"+userInstance?.address?.id

It gave me -->1

What is the reason behind that?

4

1 回答 1

0

跨映射的关联可能不会起作用。id 起作用的原因是因为 grails 在关联的惰性对象上使用了一个偷偷摸摸的技巧来防止开销。如果所需要的只是关联的 id,它通常存储在User对象(对于上面的示例)中的一个字段中,可能类似于address_id. 如果你只请求 userInstance.address.id ,那么 GORM 就足够聪明,除非真的需要,否则不会去获取整个地址记录(惰性)。

于 2014-02-18T20:08:44.183 回答