-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate mapping a Map property
PostPosted: Tue Jun 19, 2007 10:48 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 2:46 pm
Posts: 32
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)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 19, 2007 6:56 pm 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
http://www.hibernate.org/hib_docs/v3/re ... -onetomany

which shows an example:

Code:
<map name="parts"
        cascade="all">
    <key column="productId" not-null="true"/>
    <map-key formula="partName"/>
    <one-to-many class="Part"/>
</map>


Please rate if that helped.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 10:25 am 
Beginner
Beginner

Joined: Mon Nov 20, 2006 2:46 pm
Posts: 32
Thanks ouzo that is exactly what I was looking for.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.