Hi,
I have a problem with an inefficient query that involves
a collection of a generic type which has a lot of subtypes (mapped with <joined-subclass>).
Example :
I have a library which has a collection of medias. Media are implemented as books, movies,etc.
I am unable to retrieve a media of a library of a given type efficiently.
Hibernate use outer joins for each subtypes and when there are numerous
subtypes, the query is really slow.
Each concrete media like book is an entity mapped with a <joined-subclass>.
The problem is i am unable to retrieve efficiently a media of a given type for a library.
I tried the following HQL query:
Code:
" select lib.medias from Library lib join lib.medias media where lib.id="+libraryId+" and media.class="+mediaClass.getName();
But it only works when a discriminator is declared for the parent class
and subclasses are mapped using a
<subclass name="..." discriminator-value="book">
<join table="book">
<key column="media_id">
...
</join>
</subclass>
which is not my case (see below mapping files).
Do you know how to force hibernate not to perform a outer-join on
each subtypes when i am only interested in a given single subtype ?
Hibernate version: 3.0.5 Mapping documents:
Code:
<hibernate-mapping>
<class table="media" name="javaeesamples.hibernate.model.Media">
<id access="field" name="id" column="media_id">
<generator class="increment"/>
</id>
<discriminator column="media_type"/>
<property name="title" length="100" column="title" access="field" not-null="true"/>
<property name="author" length="100" column="author" access="field" not-null="true"/>
<property name="releaseDate" column="releasedate" access="field" not-null="true"/>
<joined-subclass name="javaeesamples.hibernate.model.Album" table="album">
<property name="tracksNum" column="tracksnum" access="field"/>
</joined-subclass>
<joined-subclass name="javaeesamples.hibernate.model.Book" table="book" >
<property name="chapsNum" column="chapsnum" access="field"/>
</joined-subclass>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class table="library" name="javaeesamples.hibernate.model.Library">
<id access="field" name="id" column="id">
<generator class="increment"/>
</id>
<bag cascade="all" access="field" lazy="true" table="media" name="medias">
<key column="libraryid"/>
<one-to-many class="javaeesamples.hibernate.model.Media"/>
</bag>
<property name="owner" length="100" column="owner" access="field" not-null="true"/>
</class>
</hibernate-mapping>