Currently using hibernate 3.2. I have a mapping that Canvas has a collection of Textbox and PhotoHole.
Textbox or PhotoHole is actually subclass of CanvasWidget.
I am using table per subclass approach. One Canvas has unidirection one-to-many Textboxes and PhotoHoles.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="Canvas" lazy="true"
proxy="Canvas"
table="canvas">
<id name="id" access="field" type="long">
<generator class="MultiDBKeyGenerator"></generator>
</id>
<version name="version" column="version" type="long"/>
<property name="width" type="double" not-null="true"/>
<set name="photoHoles" lazy="false" cascade="all"
access="field"
table="photo_hole">
<key column="canvasid" not-null="true"/>
<one-to-many class="PhotoHole"/>
</set>
<set name="textboxes" lazy="false" cascade="all"
access="field"
table="textbox">
<key column="canvasid" not-null="true"/>
<one-to-many class="Textbox"/>
</set>
</class>
<class name="CanvasWidget" lazy="true"
proxy="CanvasWidget"
table="canvas_widget">
<cache usage="read-write"/>
<id name="id" access="field" type="long">
<generator class="MultiDBKeyGenerator"></generator>
</id>
<discriminator column="widget_type" type="string" length="8"/>
<version name="version" column="version" type="long"/>
<property name="width" type="double" not-null="true"/>
<subclass name="Textbox" discriminator-value="TB">
<join table="textbox">
<key column="widgetid" not-null="true"/>
<property name="verticalJustification" column="vert_justify" type="string" length="30" not-null="true"/>
</join>
</subclass>
<subclass name="PhotoHole" discriminator-value="PH">
<join table="photo_hole">
<key column="widgetid" not-null="true"/>
<property name="photoUri" column="photo_uri" type="string" length="512"/>
</join>
</subclass>
</class>
</hibernate-mapping>
It generates the DDL that we want. Notice that "canvasid" column is in "canvas_widget" table.:
create table canvas (
id bigint not null,
version bigint not null,
width double not null,
primary key (id)
);
create table canvas_widget (
id bigint not null,
version bigint not null,
width double not null,
canvasid bigint not null,
primary key (id)
);
create table textbox (
widgetid bigint not null,
vert_justify varchar(30) not null,
primary key (widgetid)
);
create table photo_hole (
widgetid bigint not null,
photo_uri varchar(512),
primary key (widgetid)
);
But I need to add a collection "set" to the Textbox subclass. And I do not know how to do this in above mapping using <join> format. Since <join> does not allow <set> as child element. So I changed to following format for second class mapping in the file.
<class name="CanvasWidget" lazy="true"
proxy="CanvasWidget"
table="canvas_widget">
<cache usage="read-write"/>
<id name="id" access="field" type="long">
<generator class="MultiDBKeyGenerator"></generator>
</id>
<version name="version" column="version" type="long"/>
<property name="width" type="double" not-null="true"/>
<joined-subclass name="Textbox" table="textbox">
<key column="widgetid" not-null="true"/>
<property name="verticalJustification" column="vert_justify" type="string" length="30" not-null="true"/>
<!--
I need to add a collection here.
<set name="availableFonts" lazy="false" cascade="all" access="field" table="available_font">
<cache usage="read-write"/>
<key column="widgetid" not-null="true"/>
<one-to-many class="AvailableFont"/>
</set>
-->
</joined-subclass>
<joined-subclass name="PhotoHole" table="photo_hole">
<key column="widgetid" not-null="true"/>
<property name="photoUri" column="photo_uri" type="string" length="512"/>
</joined-subclass>
</class>
The problem is that Hibernate now put the "canvasid" into "photo_hole" and "textbox" table instead of "canvas_widget" table. Is this the right thing?
create table canvas_widget (
id bigint not null,
version bigint not null,
width double not null,
primary key (id)
);
create table textbox (
widgetid bigint not null,
vert_justify varchar(30) not null,
canvasid bigint not null,
primary key (widgetid)
);
create table photo_hole (
widgetid bigint not null,
photo_uri varchar(512),
canvasid bigint not null,
primary key (widgetid)
);
Can anyone please help me on this? I can not think of any other better way to get the solution.
|