hibernate的继承之discriminator

hibernate中,通过discriminator可以将一个字段的不同值映射成不同的子类,实现继承关系。

写法如下:

   <class name = "Question" table = "t_question">

       <id name = "quId" type = "integer">

           <column name = "t_id"/>

               <generator class = "identity">

       </id>

       <discriminator type = "string" column = "t_type"/>

       

       <property name = "name" type = "string" column = "t_name"/>

       <property name = "level" type = "string" column = "t_level"/>


        //表示t_ype的值为"c"时,对应的子类是ChoiceQuestion,子类的属性是options

       <subclass name = "ChoiceQuestion" discriminator-value = "c">

           <property name = "options" column = "t_options" type = "string">

        </subclass>

       

        //表示t_ype的值为"e"时,对应的子类是EssayQuestion,子类的属性是desc

       <subclass name = "EssayQuestion" discriminator-value = "e">

           <property name = "desc" column = "t_desc" type = "string">

        </subclass>

   </class  


你可能感兴趣的:(Hibernate)