2012-03-07 59 views
0

我有一個表(學生)的列stu_id,stu_name,favourite_subject,subject_id需要一個Hibernate的ORM映射想法

favourite_subject會像數學的預定值,英語等 如果值是數學,subject_id將有表格:maths_subject的ID。

含義,根據favourite_subject值,subject_id將指向相應主題的ID。

我該如何在Hibernate ORM框架中實現。

<hibernate-mapping package="com.sample.project"> 
     <class name="StudentDTO" table="student"> 
      <meta attribute="class-description">class to hold student data</meta> 
      <id name="id" type="long" column="stu_id"> 
       <generator class="increment"/> 
      </id> 
      <discriminator column="favourite_subject" type="string"/> 
      <property name="studentName" column="stu_name" not-null="true"/> 

      <!--?? here i don't know how to proceed. ??--> 
      <subclass name="??????" discriminator-value="Maths"> 
       ???? 
      </subclass> 
     </class> 
    </hibernate-mapping> 

所有科目都會有自己的表&自己對應的實體類。

+0

接合子類或亞類的哪一個適合在這裏嗎? – krishna 2012-03-08 06:13:35

回答

0

假設下面的類結構

class Student 
{ 
    private Subject favoriteSubject; 
} 

class MathSubject : Subject 
{ 
} 
class EnglishSubject : Subject 
{ 
} 

相應的映射

<hibernate-mapping package="com.sample.project"> 
    <class name="Student" table="student"> 
     <meta attribute="class-description">class to hold student data</meta> 
     <id name="id" type="long" column="stu_id"> 
      <generator class="increment"/> 
     </id> 
     <property name="studentName" column="stu_name" not-null="true"/> 

     <any name="subject" id-type="Integer" meta-type="String" cascade="all"> 
      <meta-value value="math" class="MathSubject"/> 
      <meta-value value="english" class="EnglishSubject"/> 
      <column name="favourite_subject"/> 
      <column name="subject_id"/> 
     </any> 
    </class> 
</hibernate-mapping>