當我創建一個將其嵌入到其他類的通用類時,我想添加一個由公式定義的瞬態屬性,但我不知道如何實現此功能。下面是我的源代碼: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)...」語句。
我不知道如何在嵌入類中使用公式。
你能幫我解決這個問題嗎?
感謝您的回覆Stefan。但是「公式」屬性實際上是由Grails支持的,並且在我使用Grails 1.3.7時,我習慣使用它。我的問題是我不知道如何使它在一個嵌入類中工作,因爲它仍然適用於特定的域類。 –
「公式」選項確實存在,它在名爲[「derived properties」(http://grails.org/doc/latest/guide/GORM.html#derivedProperties))中進行了說明。 –