无法在休眠中获取父数据

时间:2013-03-14 12:54:33

标签: java oracle hibernate

您好hibernate有很多关系问题。我有两个表有很多对很多,所以我又引入了一个表来保存两个表之间的关系。在我的配置(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>

1 个答案:

答案 0 :(得分: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>