Hi all,
any advice would be appreciated!!
I have a hierarchy of document templates and a hierarchy of documents:
public class Template {
...
}
public class LetterTemplate extends Template {
...
}
public class OfficialLetterTemplate extends LetterTemplate {
...
}
public class CasualLetterTemplate extends LetterTemplate {
...
}
public class ContractTemplate extends Template {
...
}
public class EmploymentContractTemplate extends ContractTemplate {
...
}
and so on - can be very "bushy" hierarchy
And we have the analogous hierarchy of documents:
public class Document {
private Template template;
public Template getTemplate() { return template; }
public void setTemplate (Template template) { this.template = template; }
}
public class Letter extends Document {
...
}
public class OfficialLetter extends Letter {
...
}
public class CasualLetter extends Letter {
...
}
public class Contract extends Document {
...
}
public class EmploymentContract extends Contract {
...
}
Originally my mapping looked like:
<class name="Template" table="template">
<id name="id" type="long" column="id">
<generator class="sequence">
<param name="sequence">template_seq</param>
</generator>
</id>
<version name="version" column="version" type="int"/>
..........................................
<joined-subclass name="LetterTemplate" table="letter_template">
<key column="template_id"/>
................................................................
</joined-subclass>
</hibernate-mapping>
<class name="Document" table="document">
<id name="id" type="long" column="id">
<generator class="sequence">
<param name="sequence">document_seq</param>
</generator>
</id>
<version name="version" column="version" type="int"/>
................................................................
<many-to-one name="template" class="Template" cascade="all" column="template_id"/>
<joined-subclass name="Letter" table="letter">
<key column="document_id"/>
................................................................
</joined-subclass>
</hibernate-mapping>
And when I executed session.get(OfficialLetter.class, id) the resulting query was really GIANT and killed DB2 with the message:
Query is too long or too complex:)))))))
The reason is that when Hibernate loads template for the document it DOES NOT KNOW which subtype of template to load. And it does all the possible chains of joines in template hierarchy.
The alternative approach is to map template in CONCRETE documents:
<class name="Document" table="document">
<id name="id" type="long" column="id">
<generator class="sequence">
<param name="sequence">document_seq</param>
</generator>
</id>
<version name="version" column="version" type="int"/>
................................................................
<joined-subclass name="Letter" table="letter">
<key column="document_id"/>
................................................................
<joined-subclass name="OfficialLetter" table="official_letter">
<key column="letter_id"/>
................
<many-to-one name="template" class="OfficialLetterTemplate" cascade="all" column="official_letter_template_id"/>
</joined-subclass>
</joined-subclass>
</hibernate-mapping>
The disadvantage of this approach, however, is that we have to have a template id column in every leaf document table instead of having it in just root document table.
Could you please advice the best way to map these classes??
Thanks,
Andrey
|