【问题标题】:Grails 3 domain composition using @Delegate使用 @Delegate 的 Grails 3 域组合
【发布时间】:2016-11-02 05:26:19
【问题描述】:

在我的应用程序中,我有一组 1000 个左右的“HouseProto”域对象,它们具有我在“HouseInstance”对象中需要的一堆静态属性。

在规模上,可能有大量的 HouseInstance 对象,每个对象都以 HouseProtos 为模型。以下是我尝试建模 HouseInstance 域对象的方法。

class HouseInstance {
    @Delegate
    HouseProto houseProto
    User agent
    static belongsTo=[world: World]
    static constraints = {
        agent nullable:true
    }
}

HouseProto 有很多字段,例如“squareFeet”和“bedroomCount”等。 我使用了@Delegate 注释,因为我希望能够做类似的事情

houseInstance.streetAddress

而不是

houseInstance.houseProto.streetAddress

但这在编译时失败。下面的控制台输出的末尾是对 HouseProto(一个 hasMany 集)的“功能”字段的引用,这表明这可能与它有关。不过,删除委托注释,一切正常。 (Feature 是属于 HouseProto 的域类。)

我的问题只是 @Delegate 注释是否与域类不兼容,因为它出于某种原因干扰了 GORM?如果可以编译,它似乎会做我想做的事。

HouseProto 看起来或多或少像这样:

class HouseProto {
    def houseService

    String streetAddress
    Owner owner
    Integer sqft
    Double acreage
    Integer bedroom
     ...
    Double kitchenQuality =0
    Double loanBalance =0
    Neighborhood neighborhood

    String toString() {
        "$streetAddress"
    }
    static hasMany = [features: Feature]
    static constraints = {
        streetAddress nullable: false, unique: true;
        owner nullable: true
        sqft nullable: false
        neighborhood nullable: false

    } 
  }

运行时控制台输出以此开头:

ERROR org.springframework.boot.context.embedded.tomcat.TomcatStarter - Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'grailsCacheFilter': Cannot create inner bean '(inner bean)#5ec1152d' of type [grails.plugin.cache.web.filter.simple.MemoryPageFragmentCachingFilter] while setting bean property 'filter'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#5ec1152d': Unsatisfied dependency expressed through method 'setUrlMappingsHandlerMapping' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'urlMappingsHandlerMapping': Unsatisfied dependency expressed through method 'setWebRequestInterceptors' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openSessionInViewInterceptor': Cannot resolve reference to bean 'hibernateDatastore' while setting bean property 'hibernateDatastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.MappingException: collection element mapping has wrong number of columns: com.grapevine.negotiator2.HouseInstance.features type: object

与目标VM断开连接,地址:'127.0.0.1:57570',传输:'socket'

ERROR org.springframework.boot.SpringApplication - Application      startup failed

【问题讨论】:

  • 在黑暗中野蛮刺伤:您是否尝试将 HouseProto 中的所有属性设置为 HouseInstance 中的瞬态?如果可行,您最好自己在 HouseInstance 中生成 getter 并将它们标记为瞬态。或者在下一次更改 HouseProto 时,您会看到另一个神秘错误。我根本不喜欢这样的“IDE 生成”代码,但这种情况可能证明它是合理的,因为隐式 DB-Model 生成对于使用它的开发人员来说会更加透明。
  • 我想过这一点,但似乎可能比仅使用 house instance.proto 更有效。我也在考虑用missingMethod实现,所以你可以做houseInstance.squareFeet(),它可以返回houseInstnce.proto.squarefeet。打字少,但我想知道它是否非常低效。

标签: grails grails-orm


【解决方案1】:

据我所知,@Delegate 在域中不受支持,使用 HouseInstanceHouseProto 都实现的特征可能会更幸运

【讨论】:

  • 好吧,我猜你是否不知道,这是一个尽可能明确的答案!谢谢。
猜你喜欢
  • 2016-04-10
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
  • 2019-06-15
  • 2011-09-11
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多