使用 @Delegate 的 Grails 3 域组合

Grails 3 domain composition using @Delegate

在我的应用程序中,我有一组大约 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 集)的 "features" 字段的引用,这表明这可能与它有关。不过,删除委托注释,一切正常。 (特征是属于 HouseProto 的域 class。)

我的问题很简单,@Delegate 注释是否与 Domain classes 不兼容,因为它出于某种原因干扰了 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

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