-->
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: Parent child relationship mapping and inheritance trees
PostPosted: Sat Sep 18, 2010 11:44 am 
Newbie

Joined: Sat Sep 18, 2010 11:09 am
Posts: 2
I was wondering what the best mapping strategy is when both parent and child classes belong to inheritance trees.

E.g.

CHILD HIERARCHY (one table per class hierarchy)
Row --> abstract base class with (id, description, quantity)
InvoiceRow --> subclass with "price" bigdecimal field
ReturnRow --> subclass with "reason" string field

PARENT HIERARCHY (one table per class hierarchy)
Document --> abstract base class with a List<Row>
Invoice --> subclass with a List<InvoceRow>
Return --> subclass with a List<ReturnRow>

I can think about two approaches to handle this:

1) Set up the mappings in the base classes, so
"List<Row>" in the Document class
"Document parent" in the Row class

then,
- each Document subclass has its getRows, addRow, removeRow methods which enforce the right type (InvoiceRow, ReturnRow)
- each Row subclass has its getDocument method which returns the right subclass (Invoice, Return), using covariant return types (unchecked cast is needed)

2) Set up the mappings in the concrete classes
"List<InvoiceRow>" in the Invoice class, "List<ReturnRow>" in the Return class
"Invoice parent" in the InvoiceRow class, "Return parent" in the ReturnRow class

then,
- no unchecked cast is needed (the correct type is enforced by the mapping)
- I loose the ability to ask a Document reference for its Rows or a Row for its parent Document
- *many* repeated mappings are needed if I have many concrete subclasses

Maybe, the best solution would be:

class Document <R extends Row> with List<R>
class Invoice extends Document<InvoceRow>
class Return extends Document<ReturnRow>

but that's not possible with Hibernate, right?

thanks,
marco


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.