• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

table-per-subclass conceptual issue

 
Sujan Pradhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've got a table named 'image_table' which inherits from 'content_table'; I've followed the table-per-subclass inheritance mapping like this for it:



I've used 'image_id' as a PK in 'image_table'; I am forced to use this since 'content_id_fk' repeats in 'image_table'.

The problem I am having is that during insertion hibernate throws a 'ConstraintViolationException' exception if the 'content_id_fk' repeats; other words it won't let me insert a non-uniqe key (albeit 'image_id' is a PK in 'image_table').

Is the conceptual aspect of making this a table-per-subclass inheritance mapping wrong, or what would be a solution so that I am able to insert non-unique 'conent_id' as long as it exists in 'content_table'?

TIA.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the whole inheritance hierarchy the contentID field is the PK.
if you make another field (imageId) a PK, then your hierarchy has two PKs. one PK in super class and another PK is sub class.
no mapping startegy allows you to do this. if you really need imageId as a separate field then make it UNIQUE.


pascal
 
Sujan Pradhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pascal.

That's what I was afraid of; since more than one record in image_table contains the same contentID PK, inheritance mapping strategy per hibernate isn't probably appropriate. I've opted for one-to-many association mapping and this works fine.

Now, I am confused whether my domain model wasn't correct to begin with or is it because of hibernate that I had to change. In terms of inheritance- is it reasonable to assume that image_table can have many instances(rows of records) which have a PK inherited from content_table? If I were to do this using JDBC will it work?

Conceptually speaking, my thought is that image_table has a 'is a' relationship with content_table, however I am being forced to use 'has a' relationship instead. Clarification and a probable solution would be much appreciated.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this sounds like a one-to-many relation not a inheritance.

is ImageVO really an extension to ContentVO ? or is ContentVO having a List/Set of ImageVO ?

from what you tell me:
since more than one record in image_table contains the same contentID PK


it is a "has-a" relationship. otherwise it would be that several subclasses (ImageVO) share the superclass part (ContentVO) ?

if your ContentVo needs to know all (zero or more) it's ImageVO and all the ImageVO needs to know its (one) ContentVO then you can map it as bi-directional parent-child: one-to-many and one-to-one.


hope this is answer is clearing up things and not confusing you... i would love to see a drawing tool on java ranch :-)

pascal
 
Sujan Pradhan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification; it makes a lot of sense

Cheers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic