Hi,
It looks like using the "join" fetch strategy might be dependent on indexing the proper columns? The bidirectional many-to-one mapping I have below will work, but if I remove the indexing on Activity's FLOW_ID column, I get an "illegal access to loading collection error" (the error would come up on the query in my code below). I think this is also related to the fact that I use a property accessor for handling the associations between classes because in our system Objects don't have direct references to each other (i.e. a Java reference). Instead when one class needs to keep an association to another we do it by way of the same ID that Hibernate uses to identify entities. Which is to say ElementReferenceAccessor translates from IDs to Objects in its get method and from Objects to IDs in its set method. And ElementReferenceList translates between lists of IDs and lists of Objects.
Is it true that fetch="join" and indexing columns are related?
Also there were a bunch of places like this in my mapping files that used to be done by the default fetching strategy, which was slow. I made the change to speed things up (trying to avoid n+1 selects), but actually things got a little bit slower. We are trying to load everything in at once (i.e. not lazy) because we are using the database to save the state of our entire model and reload it later, rather than just a subset of the model. So I thought it would be a lot faster to do everything with one join statement rather than many selects. Does anyone know why this way would be slower than the default fetching strategy?
Finally, is there a way for me to have Hibernate just reference the IDs for associations, rather than the whole entity object? i.e. when I use Hibernate associations (many-to-one, many-to-many) Hibernate will reconstitue the entire entity object being referenced but all I need is the ID of that entity object.
Hibernate version:
3.0.5
Mapping documents:
<joined-subclass
name="Activity"
extends="Task"
table="ACTIVITIES" lazy="false">
<key column="ACTIVITY_ID"/>
<many-to-one name="flow" column="FLOW_ID" fetch="join" index="flowId"
class="Flow"
access="ElementReferenceAccessor"/>
</joined-subclass>
<joined-subclass name="Flow"
extends="Task"
table="FLOWS" lazy="false">
<key column="FLOW_ID"/>
<bag name="activities" inverse="true" lazy="false" fetch ="join"
access="ElementReferenceListAccessor">
<key column="FLOW_ID"/>
<one-to-many
class="Activity"/>
</bag>
</joined-subclass>
Code between sessionFactory.openSession() and session.close():
List<ModelElement> mes = (List<ModelElement>) session.createQuery("From ModelElement").list();
for(ModelElement me : mes) {
addElementToModel(me);
}
Name and version of the database you are using:
HSQL 1.8
|