-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: problem with table-per-class-hierarchy strategy
PostPosted: Mon Mar 01, 2004 2:04 pm 
Beginner
Beginner

Joined: Fri Oct 24, 2003 10:45 am
Posts: 22
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.