We have a problem with composite keys and hibernate.
Below i have attached the database design and parts from the hibernate mapping files.
When i create a new article i want to set the vendor, so i set the vendor in the article also i add the article to the vendors collection of articles.
But the vendor id is never written into the database.
What i am doing wrong?
Thanks in advance,
Thomas
I have a database design like this (shorten to show only the point):
-- LOCATION TABLE --
Code:
create table location_interface (
location_id varchar(20) not null,
city varchar(40),
);
alter table location_interface add constraint location_interface_pk primary key (location_id);
-- VENDOR TABLE --
Code:
create table vendor_interface (
vendor_id varchar(20) not null,
location_id varchar(20) not null,
vendor_name varchar(60),
);
alter table vendor_interface add constraint vendor_interface_pk primary key (vendor_id,location_id);
alter table vendor_interface add constraint vendor_if_location_fk foreign key (location_id) references location_interface (location_id);
-- ARTICLE TABLE --
Code:
create table article_interface (
article_id varchar(20) not null,
location_id varchar(20) not null,
description varchar(80),
vendor_id varchar(20),
);
alter table article_interface add constraint article_interface_pk primary key (article_id,location_id);
alter table article_interface add constraint article_if_location_fk foreign key (location_id) references location_interface (location_id);
alter table article_interface add constraint article_if_vendor_fk foreign key (vendor_id,location_id) references vendor_interface (vendor_id,location_id);
I hope now you can see the releation of the entities.
Now the parts in the xml files regarding the relations:
-- LOCATION --
Code:
<id
name="locationId"
type="java.lang.String"
column="LOCATION_ID"
>
<generator class="assigned" />
</id>
<set
name="articleInterfaces"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="LOCATION_ID" />
</key>
<one-to-many
class="ArticleInterface"
/>
</set>
<set
name="vendorInterfaces"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="LOCATION_ID" />
</key>
<one-to-many
class="VendorInterface"
/>
</set>
-- VENDOR --
Code:
<composite-id name="comp_id" class="VendorInterfacePK">
<key-property
name="vendorId"
column="VENDOR_ID"
type="java.lang.String"
length="20"
/>
<key-property
name="locationId"
column="LOCATION_ID"
type="java.lang.String"
length="20"
/>
</composite-id>
<many-to-one
name="locationInterface"
class="LocationInterface"
update="false"
insert="false"
>
<column name="LOCATION_ID" />
</many-to-one>
<set
name="articleInterfaces"
lazy="true"
inverse="true"
cascade="none"
>
<key>
<column name="VENDOR_ID" />
<column name="LOCATION_ID" />
</key>
<one-to-many
class="ArticleInterface"
/>
-- ARTICLE --
Code:
<composite-id name="comp_id" class="ArticleInterfacePK">
<key-property
name="articleId"
column="ARTICLE_ID"
type="java.lang.String"
length="20"
/>
<key-property
name="locationId"
column="LOCATION_ID"
type="java.lang.String"
length="20"
/>
</composite-id>
<many-to-one
name="locationInterface"
class="LocationInterface"
update="false"
insert="false"
>
<column name="LOCATION_ID" />
</many-to-one>
<many-to-one
name="vendorInterface"
class="VendorInterface"
not-null="true"
insert="false"
update="false"
>
<column name="VENDOR_ID" />
<column name="LOCATION_ID" />
</many-to-one>