子类和hql

时间:2012-02-14 13:48:56

标签: java hibernate polymorphism hql

我有一个扩展它的类和子类。 像这样:

    @Table
    @Entity
    class Cat{



    class DomesticCat extends Cat{
       LitterBox litterBox;

    //getter and setters


    }

    class TigerCat extends Cat{
       HuntingStyle huntingStyle;

    //getter and setters
    }


}

我想用hql查询。

例如,我想找到有“蓝色”彩色LitterBox的猫

我应该在DomesticCat和TigerCat课程之前添加@entity和@Table注释吗?

数据库中没有像litterBox和huntingStyle这样的子类特定字段的列。

由于

1 个答案:

答案 0 :(得分:1)

如果您想要保留DomesticCatTigerCat,则需要添加@Entity。您可以使用@Inheritance批注定义子类的映射方式 - 对于单个表,使用@Inheritance(strategy=SINGLE_TABLE)

如果你不添加它,

@Table将被默认。