-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Unique index and foreign keys with union-subclass
PostPosted: Thu Feb 25, 2010 8:10 am 
Beginner
Beginner

Joined: Mon Aug 23, 2004 12:44 am
Posts: 25
Given,
Abstract ClassA.
- id property
- property-1
- property-2
- many-to-one to ClassX
ClassB extends ClassA
- id property
- property-3
ClassC extends ClassA
- id property
- property-4
ClassX
-id property
-property-5

Above situation is mapped using three hibernate hbm xml files:
- one for abstract ClassA, mapped with attribute abstract="true".
- one for concrete ClassB, mapped with union-subclass element and attribute extends="ClassA".
- one for concrete ClassC, mapped with union-subclass element and attribute extends="ClassA".

ClassA's mapping file contains a mapping for property-1 and 2, mapped to e.g. COLPROP1 and COLPROP2,
and a many-to-one mapping to Class/Table X.
ClassB's mapping file contains a mapping for property-3, mapped to COLPROP3.
ClassC's mapping file contains a mapping for property-4, mapped to COLPROP4.

Hibernate generates the right schema:
- One table for ClassB with four columns: for id, property-1, property-2, property-3 and a column for holding the key to table for ClassX
- One table for ClassC with four columns: for id, for property-1, property-2, property-4 and a column for holding the key to table for ClassX
- One table for ClassX with two columns: for id, for property-5

However I want to let Hibernate generate as many indexes and constraints as possible, so that my schema is as complete as possible.
Therefore I want to add where appropriate unique indexes (over multiple properties/columns) and foreign-key contraints.

Given that I have a union-subclass situation where some of the properties and associations are specified in the abstract base class mapping.

How and where should I specify a unique index e.g. "I1" on properties property-1 (defined in ClassA's hbm file) ánd property-2 (defined in ClassB's hbm file) ?
How and where should I specify the foreign key for the many-to-one association specified in the abstract base class ?

In my opinion the above is only possible if I remove property-1 and the many-to-one association from abstract ClassA's mapping file, and duplicate property-1 and the many-to-one in each of the child mapping files. Only then will I be able to specify the unique index and the foreign-key.

Am I right, or is there another way I can achieve the above without duplicating properties and associations that in fact belong in the abstract parent mapping ?

Any advice would be welcome.
Thanks,
EDH


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.