【问题标题】:GrailsDomainBinder.getMapping(Doamin) is returning null for non persisted domain in Grails-3.2.4GrailsDomainBinder.getMapping(Doamin) 在 Grails-3.2.4 中为非持久域返回 null
【发布时间】:2017-02-01 12:45:56
【问题描述】:

我有两个域。

class ABC{

    String xyz

    static mapping = {
        discriminator column: 'ABC_TYPE_ID'
    }
}

class XYZ extends ABC{

    static mapWith = "none"

    static mapping = {
        discriminator value: 3
    }   
}

在 Grails 2.5.5 中,以下行工作得非常好,但是在进行了所需的更改后,它在 Grails-3.2.4 中不起作用并且给出了 null。

Mapping mapping = new GrailsDomainBinder().getMapping(XYZ) //Works fine Grails 2.5.5

Mapping mapping = GrailsDomainBinder.getMapping(XYZ) // Giving null in Grails-3.2.4

请帮助我从 XYZ 域中获取映射对象。

我的发现:

我在 application.yml 中使用带有注释 #dbCreate:'' 的 oracle db。

在 XYZ 域中评论 static mapWith = "none" 后,我得到了 Mapping 对象,并且由于 #dbCreate:'' 而未在 DB 中创建相应的表.

这种方法有什么问题吗?如果是,请为我提供替代解决方案。

【问题讨论】:

    标签: hibernate grails groovy grails-orm grails3.2.0


    【解决方案1】:

    在最新版本的 GORM 中,静态部分已被删除。你需要使用MappingContext 对象来获得你想要的:

    MappingContext mappingContext = .. // can be injected via Spring
    Mapping mapping = (Mapping)mappingContext.getPersistentEntity(Foo).mapping.mappedForm
    

    http://gorm.grails.org/latest/hibernate/api/org/grails/datastore/mapping/model/MappingContext.html

    【讨论】:

      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      相关资源
      最近更新 更多