I am having the exact same issue w/Hibernate mappings on objects which work over Oracle 10g. In my case, the difference in performance is a couple of minutes sans the index, and under 2 seconds w/it(I manually created an index based on suggestions from Oracle's Performance Analyser). A table scan is also being performed when the index is not present.
On one side of the bi-directional relationship, the mapping is as follows:
Code:
<hibernate-mapping>
<class name="com.xrite.ind.core.Color" table="tblColor">
<id name="Id" type="long">
<column name="colorID" />
<generator class="native" />
</id>
<property name="name" column="name" type="string" not-null="true" index="IDX_COLOR_NAME" />
<property name="description" column="description" type="string" />
. . .
<joined-subclass name="com.xrite.ind.core.Standard" table="tblStandards">
<key column="standardID" on-delete="cascade"/>
<property name="masterPanel" column="masterPanel" type="boolean" />
<property name="panelAutoName" column="panelAutoName" type="boolean" />
<set name="samples" lazy="true" cascade="save-update" inverse="true" sort="com.xrite.ind.core.ColorDateSort" >
<key column="standardID" on-delete="cascade"/>
<one-to-many class="com.xrite.ind.core.Sample" />
</set>
<list name="tolerances" table="tblStandardTolerances" fetch="subselect" cascade="save-update" lazy="false" >
<key column="standardID" not-null="true" />
<index column="tolerance_index" />
<many-to-many column="ToleranceID" class="com.xrite.ind.core.tolerances.Tolerance" />
</list>
<many-to-one name="shadeSortInfo" class="com.xrite.ind.core.ShadeSortInfo" cascade="save-update" unique="true" index="IDX_COLOR_SSI" lazy="false" >
<column name="ShadeSortInfoID" />
</many-to-one>
</joined-subclass>
</class>
</hibernate-mapping>
On the other side, we have this snippet:
Code:
<set name="Standards" table="tblStandardTolerances" inverse="true" cascade="save-update">
<key column="ToleranceID" />
<many-to-many column="StandardID" class="com.xrite.ind.core.Standard"/>
</set>
With this relationship and mapping, the PK of the tblStandardTolerances table is StandardID. If I change the PK to be a composite(StandardID, ToleranceID), performance is still slow(table scan). If I switch the inverse side around, this does achieve somewhat of the desired affect by making the ToleranceID the PK, however, this breaks other things in my object model.
There should be a simple way to arbitrarily assign an index to a foreign key column on a many-to-many. Currently, I have to use either straight JDBC or a database object.