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
|