Hi there,
First post here and hopefully not my last!
I'm working on a project now where we're trying to replace our lame data access layer with Hibernate. Problem is that our current inheritance model is quite tricky to capture. This is a really interesting use case for Hibernate so hopefully I'll get some great responses!
The database tables look like this:
BASEENTITY -ID -LINKID -STUFF
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 ID 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
This is a problem in hibernate. We hoped to use multiple unidirectional one to one relationships but you have to annotate both ends so the LINKID field can only take one reverse link! We then tried marking BaseEntity with Inheritance type 'table per class'. but Hibernate freaks out because it states that you can't override the 'ID' field in the subclasses. It doesn't realise they're not related!
After this the only solutions we can currently come up with are to not use Hibernate for the inheritance and actually have some delegate class which looks up the BaseEntity data for you as required e.g.
BaseEntityDelegate.lookupBaseEntityDate(BaseEntity b);//runs a sql db hit and sets values on BaseEntity
But this is pretty lame.
An elegant solution to this would be awesome!
Regards, John
|