I am running in to an issue involving a map association and sequential SQL selects. Using a Criteria query, I need to load a lot of fairly large business objects in a single SQL. By setting fetch mode to JOIN, I am able to do this for every joined table except for one mapped with the <map> tag. I can not seem to convince that tag to eagerly fetch the many-to-many key set. The manual’s entry on this (
http://www.hibernate.org/hib_docs/v3/reference/en/html/collections.html ) lists two XML attributes that I thought might help, “fetch” and “lazy.” However, no combination of the two seemed to effect this behavior.
The only reference to this issue that I was able to find is here:
http://forums.hibernate.org/viewtopic.php?p=2297304&sid=388e7011a74f6f2160d65b71e861f83a, and that user didn’t seem to have any luck resolving the problem.
See below for details. I have attempted to reduce the complexity of the object and the associated code for the sake of simplicity. Notice that there are two select statements in the generated SQL: the test case I created has a single row in the OBJECT_COUNTRY join table, resulting in this single extra select. More rows result in more selects. Is there something I should be doing differently, or is this a bug? If the latter, could anyone suggest a work-around for this?
Hibernate version: 3.0.5
Name and version of the database you are using: Oracle 9i
Mapping documents:
Code:
<hibernate-mapping>
<class name="db.DbDigitalObject" table="OBJECT_DEFINITION">
…a bunch of properties
<map name="countrySupport" table="OBJECT_COUNTRY" cascade="save-update">
<key column="OBJECT_DEFINITION_ID" />
<map-key-many-to-many column="COUNTRY_ID"
class="db.DbCountry"/>
<element column="COUNTRY_SUPPORTED" type="yes_no" />
</map>
</class>
</hibernate-mapping>
Criteria code:Code:
Criteria criteria = getSession().createCriteria(DbDigitalObject.class).
setFetchMode("countrySupport", FetchMode.JOIN);
The generated SQL (show_sql=true):Code:
select (many properties)
from OBJECT_DEFINITION this_
left outer join OBJECT_COUNTRY countrysup4_ on this_.OBJECT_DEFINITION_ID=countrysup4_.OBJECT_DEFINITION_ID
select dbcountry0_.COUNTRY_ID as COUNTRY1_0_,
dbcountry0_.COUNTRY as COUNTRY10_0_
from OBJECT_COUNTRY_LOOKUP dbcountry0_
where dbcountry0_.COUNTRY_ID=?