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>
4

1 回答 1

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>
于 2013-03-14T16:48:37.963 回答