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
|