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

formulaGrails のマニュアルによると、マッピング設定のようなものはありません。

ドメインクラスでゲッターメソッドを宣言するだけでこれを解決できます。

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"
    }
}

NB その都市は現在、別のドメイン クラスへの参照です。ドラフトでは、これはドメイン モデルをナビゲートしにくくする単なる id です。

于 2013-08-31T09:24:19.887 に答える