2013-08-30 29 views
0

當我創建一個將其嵌入到其他類的通用類時,我想添加一個由公式定義的瞬態屬性,但我不知道如何實現此功能。下面是我的源代碼:Grails:如何在嵌入類上創建公式

//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類,nhưng當我得到ID特定的用戶,並檢查在user.locationInfo.fullAddress的價值,它始終是NULL;並且生成的SQL不包含「SELECT(l_address ||','|| city.name)...」語句。
我不知道如何在嵌入類中使用公式。

你能幫我解決這個問題嗎?

回答

0

根據Grails手冊,在映射設置中沒有像formula這樣的東西。

我想通過簡單地在你的領域類聲明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。

+0

感謝您的回覆Stefan。但是「公式」屬性實際上是由Grails支持的,並且在我使用Grails 1.3.7時,我習慣使用它。我的問題是我不知道如何使它在一個嵌入類中工作,因爲它仍然適用於特定的域類。 –

+2

「公式」選項確實存在,它在名爲[「derived properties」(http://grails.org/doc/latest/guide/GORM.html#derivedProperties))中進行了說明。 –

0

我也有這個問題,我認爲你不能這樣做。

當您爲某些派生屬性定義公式時,必須將SQL與您知道的列的名稱放在一起。但是當這個類被用在嵌入式屬性中時,那個嵌入式對象的列名被改變了。

在您的案例表中,用戶具有列位置信息_l_城市,位置信息_地址。但在公式中,您使用了像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" 
    } 
} 

現在,你可以使用列User.fullAddress也HQL查詢。