2013-03-14 78 views
0

您好我有一個與多對多關係的休眠問題。我有兩個表有多對多的表,所以我介紹了一個表,它保存了兩個表之間的關係。在我的配置(hbm)文件中,我做了lazy =「false」和fetch =「join」,因爲我想在每次查詢父表時都獲取父和子信息。現在我想從第三個表中獲取父表數據它保持兩個表之間的關係)。但我無法取得父母的信息,我得到以下例外。無法在休眠狀態下獲取父數據

配置片段

Parent 1 - 
    <hibernate-mapping> 
     <class name="com.sample.Technology" table="TECHNOLOGY"> 
      <id name="technologyId" type="big_decimal"> 
       <column name="TECHNOLOGY_ID" precision="22" scale="0" /> 
       <generator class="increment" /> 
      </id> 
      <property name="technologyName" type="string"> 
       <column name="TECHNOLOGY_NAME" length="50" not-null="true" unique="true" /> 
      </property> 
      <property name="technologyDesc" type="string"> 
       <column name="TECHNOLOGY_DESC" length="500" /> 
      </property> 
      <set name="regionTechnologyCapabilities" table="REGION_TECHNOLOGY_CAPABILITY" cascade="all" inverse="true" lazy = "false" fetch="join"> 
       <key> 
        <column name="TECHNOLOGY_ID" precision="22" scale="0" not-null="true" unique="true" /> 
       </key> 
       <one-to-many class="com.sample.RegionTechnologyCapability" /> 
      </set> 
     </class> 
    </hibernate-mapping> 
Parent 2 : - 

<class name="com.sample.Region" table="REGION"> 
    <id name="regionId" type="big_decimal"> 
     <column name="REGION_ID" precision="22" scale="0" /> 
     <generator class="increment" /> 
    </id> 
    <property name="regionName" type="string"> 
     <column name="REGION_NAME" length="50" not-null="true" unique="true" /> 
    </property> 
    <property name="regionDesc" type="string"> 
     <column name="REGION_DESC" length="500" /> 
    </property> 
    <set name="regionTechnologyCapabilities" table="REGION_TECHNOLOGY_CAPABILITY" cascade="all" inverse="true" lazy = "false" fetch="join"> 
     <key> 
      <column name="REGION_ID" precision="22" scale="0" not-null="true" unique="true" /> 
     </key> 
     <one-to-many class="com.sample.RegionTechnologyCapability" /> 
    </set> 
</class> 

Relational Table 3 :- <class name="com.sample.RegionTechnologyCapability" table="REGION_TECHNOLOGY_CAPABILITY"> 
     <id name="regionTechCapbId" type="big_decimal"> 
      <column name="REGION_TECH_CAPB_ID" precision="22" scale="0" /> 
      <generator class="increment" /> 
     </id> 
     <many-to-one name="region" class="com.sample.Region" fetch="join"> 
      <column name="REGION_ID" precision="22" scale="0" not-null="true" /> 
     </many-to-one> 
     <many-to-one name="technology" class="com.sample.Technology" fetch="join"> 
      <column name="TECHNOLOGY_ID" precision="22" scale="0" not-null="true" /> 
     </many-to-one> 
    </class> 
+0

是否可以提供配置文件或簡化版的配置文件? – 2013-03-14 12:56:16

回答

0

爲什麼你手動映射連接表?使用雙向多對多,讓Hibernate處理連接表!

<hibernate-mapping> 
    <class name="com.sample.Technology" table="TECHNOLOGY"> 
     <id name="technologyId" type="big_decimal" column="TECHNOLOGY_ID"> 
      <generator class="increment"/> 
     </id> 
     <set name="regions" table="REGION_TECHNOLOGY" inverse="true"> 
      <key column="TECHNOLOGY_ID"/> 
      <many-to-many column="REGION_ID" class="com.sample.Region"/> 
     </set> 
    </class> 
</hibernate-mapping> 

<hibernate-mapping> 
    <class name="com.sample.Region" table="REGION"> 
     <id name="regionId" type="big_decimal" column="REGION_ID"> 
      <generator class="increment"/> 
     </id> 
     <set name="technologies" table="REGION_TECHNOLOGY"> 
      <key column="REGION_ID"/> 
      <many-to-many column="TECHNOLOGY_ID" class="com.sample.Technology"/> 
     </set> 
    </class> 
</hibernate-mapping> 
+0

這種雙向M2M關係映射不會幫助我的要求,因爲我想在未來添加更多專用於連接表的列。如果是這種情況,我該如何做這個映射。 – 2013-03-15 04:45:51

+0

這方面的更新? – 2013-03-22 11:34:01

相關問題