You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Vincent Frison <tu...@ohmforce.com> on 2006/07/12 18:22:16 UTC

Inheritance and foreign keys

Hi all,

I have a class A referencing another class B. My code (and database) works 
fine for a long time but now I have to do some major changes to my app: class 
A must reference class B OR class C. These two classes should share the same 
interface (or inherit from the same abstract class) but must be stored in 
distinct tables (ie. table per class).

There's no pb with OJB, I'm going to just use <extent-class> in my 
<class-descriptor> for the class A.

But what about the database and its referential integrity? Since I map each 
class of the hierarchy to distinct table there is a pb with database because 
as far as I know standard SQL foreign keys refers to only one table.

Hmm I gess it's a common and very basic question.. But what should I do if I 
want to not lose database referential integrity (ie. removing FK constraint)?

Mapping B and C class to the same table is the only solution? I guess not..
Maybe I should add anoter FK from my table A to table C and then add 
getters/setters to class A relating to the interface: the getter would return 
a class B or C, and the setter would add a class B or C (in condition that 
neither is already linked).

Thanks a lot for your good advices,

Regards,

Vincent.

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Inheritance and foreign keys

Posted by Vincent Frison <tu...@ohmforce.com>.
Le jeudi 2006 juillet 13 18:12, Thomas Dudziak a écrit :
> Hi Vincent,
>
> the main problem that you have is that you cannot define a foreign key
> that references TableA OR TableB.

That's my problem. It doesn't seem very exotic..

> Basically if you want to have database foreign keys (and if your OJB
> app is the only user of the database, then you don't need to, OJB
> handles the referencial integrity itself),

Mostly.. My OJB app is the only "user" app using the database, but sometimes 
admins use database directly with phpPgAdmin so I really want to keep 
database referential integrity because admins can do bad things. ;)

> then you basically have to 
> define a common base class for your two classes which is mapped to a
> specific table, say TableAB.
> From there you now have two options as to how to map the inheritance
> onto tables. You can either map both classes completely to this table
> or let OJB use joins.
> If the two classes have a lot of database-mapped attributes in common,
> the first solution makes most sense, esp. performance-wise.
> If they are however quite different, you probably want the second
> option, which is explained in more detail here:
>
> http://db.apache.org/ojb/docu/guides/advanced-technique.html#Mapping+Each+S
>ubclass+to+a+Distinct+Table+%28table+per+subclass%29

I have already read this usefull guide but only the 2 first solutions:
- Mapping Each Class of a Hierarchy to a Distinct Table
- Mapping Class Hierarchy on the Same Table"

The third way (ie. Mapping Each Subclass to a Distinct Table) should be the 
best to keep my database FK up. I'm going to try it.

Thanks a lot Thomas.

Regards,

Vincent.

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Inheritance and foreign keys

Posted by Thomas Dudziak <to...@gmail.com>.
Hi Vincent,

the main problem that you have is that you cannot define a foreign key
that references TableA OR TableB.

Basically if you want to have database foreign keys (and if your OJB
app is the only user of the database, then you don't need to, OJB
handles the referencial integrity itself), then you basically have to
define a common base class for your two classes which is mapped to a
specific table, say TableAB.
>From there you now have two options as to how to map the inheritance
onto tables. You can either map both classes completely to this table
or let OJB use joins.
If the two classes have a lot of database-mapped attributes in common,
the first solution makes most sense, esp. performance-wise.
If they are however quite different, you probably want the second
option, which is explained in more detail here:

http://db.apache.org/ojb/docu/guides/advanced-technique.html#Mapping+Each+Subclass+to+a+Distinct+Table+%28table+per+subclass%29

hope that helps,
Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Inheritance and foreign keys

Posted by Vincent Frison <tu...@ohmforce.com>.
Le mercredi 2006 juillet 12 18:22, Vincent Frison a écrit :
> Hi all,
>
> I have a class A referencing another class B. My code (and database) works
> fine for a long time but now I have to do some major changes to my app:
> class A must reference class B OR class C. These two classes should share
> the same interface (or inherit from the same abstract class) but must be
> stored in distinct tables (ie. table per class).
>
> There's no pb with OJB, I'm going to just use <extent-class> in my
> <class-descriptor> for the class A.
>
> But what about the database and its referential integrity? Since I map each
> class of the hierarchy to distinct table there is a pb with database
> because as far as I know standard SQL foreign keys refers to only one
> table.
>
> Hmm I gess it's a common and very basic question.. But what should I do if
> I want to not lose database referential integrity (ie. removing FK
> constraint)?
>
> Mapping B and C class to the same table is the only solution? I guess not..
> Maybe I should add anoter FK from my table A to table C and then add
> getters/setters to class A relating to the interface: the getter would
> return a class B or C, and the setter would add a class B or C (in
> condition that neither is already linked).
>
> Thanks a lot for your good advices,

Hmm maybe this post was a little out of topic because it's not directly 
related to OJB. But I think that most of OJB users should have resolved this 
basic pb that's why I posted it here..

Regards,

Vincent.

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org