I have the following classes:
Code:
public class Provider
{
private int id;
private Map<String,ProviderFee> fees = TreeMap<String,ProviderFee>();
// standard getters and setters omitted
}
public class ProviderFee
{
private String code;
private Integer fee;
// standard getters and setters omitted
}
with the following schema:
Code:
CREATE TABLE provider
(
provider_id integer NOT NULL,
CONSTRAINT provider_pkey PRIMARY KEY (provider_id)
)
CREATE TABLE provider_fee
(
provider_id integer NOT NULL,
code character varying(12) NOT NULL,
fee integer,
CONSTRAINT provider_fee_area_pkey PRIMARY KEY (process_server_id, code),
CONSTRAINT fk_pf_provider FOREIGN KEY (provider_id)
REFERENCES provider (provider_id)
)
What is the best approach to do in the Hibernate mapping for these classes with the given schema?
I tried the following:
Code:
<hibernate-mapping>
<class name="Provider" table="provider">
<!-- omit id property as irrelevant -->
<map name="fees"
table="provider_fee"
sort="natural">
<key column="provider_id" />
<map-key type="string" column="code" length="12" />
<composite-element class="ProviderFee">
<property name="code" type="string">
<column name="code" length="12" not-null="true" />
</property>
<property name="fee" type="int">
<column name="fee" />
</property>
</composite-element>
</map>
</class>
</hibernate-mapping>
But I get the following error:
org.hibernate.MappingException: Repeated column in mapping for collection: Provider.fees column: code
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:304)
at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:327)
at org.hibernate.mapping.Collection.validate(Collection.java:284)
at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:67)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1030)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1211)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:807)
(rest of stack trace snipped)