Hello,
I have problem with creation of tables by using hibernate SchemaExport tool. It seems that in case of "Table per subclass, using a discriminator" model, the tool creates duplicate column in table for parent class for bi-directional associations in child class. I put example mappings below. For these mappings, it creates two "B" column in vk_object, and vk_class_a table. Column "B" disapear form vk_object only when I remove inverse collection from "ClassB.hbm.xml" mapping. Could you adive what is wrong in such mappings?
Hibernate version: hibernate-3.2.5.ga
Mapping documents:
hibernate.cfg.xml
Code:
<hibernate-configuration>
<session-factory>
<mapping resource="GenericDbObject.hbm.xml" />
<mapping resource="ClassA.hbm.xml" />
<mapping resource="ClassB.hbm.xml" />
</session-factory>
</hibernate-configuration>
GenericDbObject.hbm.xml
Code:
<hibernate-mapping package='com.vkernel.db'>
<class name="GenericDbObject" table="vk_object" lazy="false" discriminator-value="ABSTRACT">
<id name="id" column="Id" type="long">
<generator class="native"/>
</id>
<discriminator column="ObjType" type="string" />
</class>
</hibernate-mapping>
ClassA.hbm.xml
Code:
<hibernate-mapping package='com.vkernel.db'>
<subclass name="ClassA" extends="GenericDbObject" discriminator-value="TST_CLASS_A">
<join table="vk_cls_a" >
<key column="Id" on-delete="cascade" foreign-key="vk_cls_a_pk"/>
<property name="name" column="Name" type="string" />
<many-to-one name="b" column="B" class="com.vkernel.db.ClassB" foreign-key="vk_cls_a_fk1" />
</join>
</subclass>
</hibernate-mapping>
ClassB.hbm.xml
Code:
<hibernate-mapping package='com.vkernel.db'>
<class name="ClassB" table="vk_cls_b">
<id name="id" column="Id" type="long">
<generator class="native"/>
</id>
<property name="name" column="Name" type="string" />
<set name="aset" inverse="true" cascade="all-delete-orphan" table="vk_cls_a">
<key column="B" />
<one-to-many class="ClassA" />
</set>
</class>
</hibernate-mapping>
Name and version of the database you are using:MySQL 5.0
The generated SQL (show_sql=true):Code:
create table vk_cls_a (Id bigint not null, Name varchar(255), B bigint, primary key (Id)) ENGINE=InnoDB
create table vk_cls_b (Id bigint not null auto_increment, Name varchar(255), primary key (Id)) ENGINE=InnoDB
create table vk_object (Id bigint not null auto_increment, ObjType varchar(255) not null, B bigint, primary key (Id)) ENGINE=InnoDB
alter table vk_cls_a add index vk_cls_a_pk (Id), add constraint vk_cls_a_pk foreign key (Id) references vk_object (Id) on delete cascade
alter table vk_cls_a add index vk_cls_a_fk1 (B), add constraint vk_cls_a_fk1 foreign key (B) references vk_cls_b (Id)
alter table vk_object add index FK27198C0994805294 (B), add constraint FK27198C0994805294 foreign key (B) references vk_cls_b (Id)
After removing of inverse collection from "ClassB.hbm.xml" mapping, it generates correct sqls:
Code:
create table vk_cls_a (Id bigint not null, Name varchar(255), B bigint, primary key (Id)) ENGINE=InnoDB
create table vk_cls_b (Id bigint not null auto_increment, Name varchar(255), primary key (Id)) ENGINE=InnoDB
create table vk_object (Id bigint not null auto_increment, ObjType varchar(255) not null, primary key (Id)) ENGINE=InnoDB
alter table vk_cls_a add index vk_cls_a_pk (Id), add constraint vk_cls_a_pk foreign key (Id) references vk_object (Id) on delete cascade
alter table vk_cls_a add index vk_cls_a_fk1 (B), add constraint vk_cls_a_fk1 foreign key (B) references vk_cls_b (Id)
Please, help.
Thanks,
Victor