0

当我创建一个通用类以将其嵌入到其他类时,我想添加一个由公式定义的瞬态属性,但我不知道如何实现它。这是我的源代码:

//Embedded
class LocationInfo {
    Long country, state, city
    String address, fullAddress

    static constraints = {
        country nullable: true
        state nullable: true
        city nullable: true
        address nullable: true
    }

    static mapping = {
        country column: 'l_country'
        state column: 'l_state'
        city column: 'l_city'
        address column: 'l_address'
        fullAddress formula: "SELECT (l_address || ', ' || city.name) FROM system_location city  WHERE city.id = l_city"
    }

    static transients = ['fullAddress']
}  

class SystemLocation {
    String name
    Long parentLocationId
    String level

    static constraints = {
        name blank: false, maxSize: 100
        parentLocationId nullable: true
        level inList: ['country', 'state', 'city']
    }

    static mapping = { version false }
}  

//Host
class User {
    String email
    String password
    String firstName
    String lastName
    Team team
    UserLevel userLevel
    boolean enabled = true
    boolean accountExpired = false
    boolean accountLocked = false
    boolean passwordExpired = false
    boolean teamLeader = false

    LocationInfo locationInfo
    AuditingInfo auditingInfo

    static embedded = ['locationInfo', 'auditingInfo']

    transient springSecurityService
    static constraints = {
        email blank: false, unique: true, maxSize: 200
        password blank: false, maxSize: 200
        firstName blank: false
        lastName blank: false
        team nullable: true
        teamLeader nullable: true
        locationInfo nullable: true
        auditingInfo nullable: true
    }

    static mapping = {
        team column: "team_id"
        userLevel column: "user_level_id"
    }  
}

LocationInfo嵌入到类中,当User我通过 ID 获取特定用户并检查 中的值时user.locationInfo.fullAddress,它始终为 NULL;并且生成的 SQL 不包含“SELECT (l_address || ', ' || city.name)...”语句。
我不知道如何在嵌入式类中使用公式。

你能帮我解决这个问题吗?

4

2 回答 2

0

formula根据 Grails 手册,在映射设置中没有这样的东西。

我只需在您的域类上声明一个 getter 方法即可解决此问题:

class LocationInfo {
    Long country, state, 
    SystemLocation city
    String address    // n.b. no longAddress here

    static constraints = {
        country nullable: true
        state nullable: true
        city nullable: true
        address nullable: true
    }

    static mapping = {
        country column: 'l_country'
        state column: 'l_state'
        address column: 'l_address'
    }

    static transients = ['fullAddress']

    String getFullAddress() {
       "$address $city.name"
    }
}

请注意,该城市现在是对另一个域类的引用。在您的草稿中,这只是一个 id,它使您的域模型难以导航。

于 2013-08-31T09:24:19.887 回答
0

我也有这个问题,我认为你不能那样做。

当您为某些派生属性定义公式时,您必须将 SQL 与您知道的列名放在一起。但是,当此类用于嵌入的属性时,该嵌入对象的列名称会发生​​变化。

在您的情况下,表用户有列 location_info_l_city、location_info_l_address。但是在公式中,您使用了 l_city、l_address 之类的名称......表 User 中没有这样的列。

我通过为嵌入对象的所有者添加派生属性解决了这个问题。在您的情况下,我将添加到类用户映射:

class User {
   //...
   String fullAddress
   //...
   static mapping = {
       //...
       fullAddress formula: "SELECT (location_info_l_address || ', ' || city.name) FROM system_location city  WHERE city.id = location_info_l_city"
   }
}

现在,您也可以在 HQL 查询中使用列 User.fullAddress。

于 2017-05-30T09:33:36.520 回答