Hi John,

Since we have a discriminator column, in Cayenne this can be mapped via single-table inheritance. Here are two ways of mapping with examples that I created in about 15 minutes using Cayenne 3.0 and embedded Derby DB. In both cases we map ID + LINKID as a compound PK (even though LINKID is not a PK in the DB, we'll make Cayenne think it is) :

1. Mapping as single table inheritance with flattened attributes of the joined entity.

http://people.apache.org/~aadamchik/demo/inheritance-test.0.tar.gz

Got a little problem with that - an exception on commit. LINKID is not propagated from TRADE and INSTR back to BASEENTITY. Oops... This is clearly a bug in Cayenne. Will need to fix it.

So until that I tried something else:


2. Mapping as single table inheritance with relationship from subclasses to specific entities

http://people.apache.org/~aadamchik/demo/inheritance-test.1.tar.gz

This works all the way, although requires just a bit more code to handle dependent object.

So Cayenne can handle this case (and when we fix 1., hide most DB aspects of it).

Hope this helps

Andrus




On Apr 1, 2010, at 7:25 PM, Summers, John1 wrote:

Thanks Robert,

Good point and I have made a stupid ommission in my description. In fact BASEENTITY has a column 'REF' on it which together with LINKID provides uniqueness:

BASEENTITY
ID=1, LINKID=100, REF='Trade'
ID=2, LINKID=200, REF='Instr'
(LINKID and REF together have a unique index on them)

TRADE
ID=100

INSTR
ID=200

And in the current data access layer the DAO's actually work out which type of entity they are building and and provide the correct discriminator ('Trade', 'Instr', etc) for the sql statements to load in the data to the superclass.

Cheers,
John

-----Original Message-----
From: Robert Zeigler [mailto:[email protected]]
Sent: 01 April 2010 17:05
To: [email protected]
Subject: Re: can Cayenne handle an inheritance model like this?...

How do you ensure that you don't wind up with duplicated LINKID values? Eg, if TRADE.ID assigned based on the value of LINKID, or is LINKID assigned based on the value of TRADE.ID and/or INSTR.ID?

Robert

On Thu, Apr 1, 2010 at 10:49 AM, Summers, John1 <[email protected] >wrote:

Hello Apache-Cayenne users,

I am engaged on a project where we are trying to replace a messy
bespoke Data Access Layer with a more modern ORM framework. We have
been looking at Hibernate but it cannot handle our inheritance model.
I would like to ask if Cayenne can handle such a model because if it
can we will continue and make a full assessmet of Cayenne. I will describe the model to you now:

Ok the database tables look like this:

BASEENTITY
-ID
-LINKID

TRADE
-ID

INSTR
-ID

And the Java object hierarchy is that BaseEntity is the superclass
with Trade and Instr (and others) as the subclass.

Here's the rub...

The ID fields on the 3 tables do NOT express a hierarchy. They are
unique to each table only. You cannot look up a Trade's BaseEntity by
using the trade ID and finding a matching LINKID in BaseEntity.

The connection is actually the BASEENTITY.LINKID field. The LINKID
matches IDs in the TRADE or INSTR table. Like this:

BASEENTITY
ID=1, LINKID=100
ID=2, LINKID=200

TRADE
ID=100

INSTR
ID=200

Hibernate cannot handle this. Unidirectional one to one relationships
require annotation on each end. The 'table per class' inheritance
model requires the ID fields in TRADE and INSTR to be foreign keys of
BASEENTITY, which they are not.

Look forward to any advice.
Thanks,
John




Reply via email to