如果我有像...如何在GORM GRAILS中爲域類設置公共基類型?
class Tiger{
int id
String name
}
class Giraffe{
int id
String name
}
我怎麼能有這樣他們從通用類例如延長動物,但我不需要動物表,只有長頸鹿和老虎表 所以我想能夠像Animal.findNameBy('鮑勃') 這樣的查詢這是可行的在GORM?
如果我有像...如何在GORM GRAILS中爲域類設置公共基類型?
class Tiger{
int id
String name
}
class Giraffe{
int id
String name
}
我怎麼能有這樣他們從通用類例如延長動物,但我不需要動物表,只有長頸鹿和老虎表 所以我想能夠像Animal.findNameBy('鮑勃') 這樣的查詢這是可行的在GORM?
您可以創建一個類Animal
grails-app/src/java
或grails-app/src/groovy
,然後你的域可以擴展類及其成員在Giraffe
和Tiger
域使用。
雖然我不認爲你可以在Animal
類上使用GORM查詢。 試試看並找出答案。
是的,這很容易實現。正如你所描述的那樣,請做:在你的域名文件夾中添加一個類動物,讓長頸鹿和老虎從這個類中派生出來。一切都將按需要立即工作,以便您可以從Anmial
課程中查詢所有動物。看看:http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.2.3%20Inheritance%20in%20GORM
請刪除int id
定義,因爲GORM將處理您的id。
每個層次或每個子類可以有一個表。看看:http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html#5.5.2.3%20Inheritance%20Strategies
這將生成一個Tiger表和一個Giraffe表,它們都有(除了默認的GORM id和版本列)標有Name的列。雖然長頸鹿也會有一個標有斑點的專欄,而Tiger會有一個標有條紋的專欄。
class Animal{
String name
static mapping = {
tablePerHierarchy false
}
}
class Tiger{
String stripes
}
class Giraffe{
String spots
}