我已經使用繼承在Grails中建模了我的域類,如下所示。Grails/Groovy域類的繼承性演變
abstract class Profile{
}
class Team extends Profile{
}
class User extends Profile{
}
class B{
static hasMany = [profiles: Profile]
}
時,我得到的所有曲線從B類在某些情況下,我想投一些配置文件到團隊或用戶控制器之後,但我不能,因爲我得到一個Java。 lang.ClassCastException或GroovyCastException,儘管它們保存爲Team或User(在數據庫中具有屬性類)。以下是我已經試過的方法:
def team1 = b.profiles.toList()[0] as Team
def team1 = (Team)b.profiles.toList()[0]
它工作時,我不寫任何類型的只是使用它,因爲它是一個動態語言正常。
def team1 = b.profiles.toList()[0]
但後來我永遠不知道我正在使用哪個類。無論如何,groovy或gorm會把父母班教給孩子嗎?
沒有[鑑別](http://grails.org/doc/latest/ref/Database%20Mapping/discriminator.html)爲格姆之間'選擇用戶或團隊。 gorm應該如何決定使用哪個實現? – krock 2012-07-11 10:07:58
它保存到數據庫中的同一個表中,並具有指示哪個類的類屬性。 – sgleser87 2012-07-11 10:42:52