-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to map a Map<String,String> using annotations?
PostPosted: Tue Mar 27, 2012 9:50 am 
Newbie

Joined: Tue Nov 01, 2005 12:38 pm
Posts: 12
Hello,

I'm trying to switch from XML configuration to annotation based configuration, but I'm stuck with a problem that I thought should not be that hard to solve, sadly I could not find anything about it that works.

I got a class named Event, the Event contains a Map<String,String> of the Events attributes. With the XML configuration this can be mapped easily:

Code:
        <map access="field" name="attributes" table="eventattributes" lazy="false">
            <key column="id"/>
            <index column="key" length="1024" type="string"/>
            <element column="value" length="4096" type="string"/>
        </map>


My current approach with annotations is the following:

Code:
    @ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
    @MapKeyClass(String.class)
    @AttributeOverrides({
            @AttributeOverride(name="key", column=@Column(name = "key", length = 1024)),
            @AttributeOverride(name="value", column=@Column(name = "value", length = 4096))
    })
    private Map<String,String> attributes = new TreeMap<String,String>();


Using this causes a exception I do not understand:

Code:
Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: xxx.domain.EventDO.attributes column: count
   at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:341)


@CollectionOfElements worked, but it is deprecated, and I don't know how to adjust the column sizes.

Regards,

Sven


Top
 Profile  
 
 Post subject: Re: How to map a Map<String,String> using annotations?
PostPosted: Wed Mar 28, 2012 10:39 am 
Newbie

Joined: Tue Nov 01, 2005 12:38 pm
Posts: 12
No idea anyone? ö.ö


Top
 Profile  
 
 Post subject: Re: How to map a Map<String,String> using annotations?
PostPosted: Thu Mar 29, 2012 8:05 am 
Newbie

Joined: Tue Nov 01, 2005 12:38 pm
Posts: 12
Now I found out that the following nearly works:

Code:
    @ElementCollection(fetch = FetchType.EAGER)
    @MapKeyColumn(name = "key", length = 1024)
    @Column(name = "value", length = 4069)
    private Map<String,String> attributes = new TreeMap<String,String>();


Sadly this causes a join over the attribues table, so I get duplicate results. I understand why I join causes duplicate results, this is described in the FAQ, but I do not understand why Hibernate tries to fill this Map with a join at all. The XML based configuration I used first causes a query to the attributes table for each row returned, so there is no join and there are no duplicate results.

Regards,

Sven


Top
 Profile  
 
 Post subject: Re: How to map a Map<String,String> using annotations?
PostPosted: Thu Mar 29, 2012 9:44 am 
Newbie

Joined: Tue Nov 01, 2005 12:38 pm
Posts: 12
I found the solution:

Code:
    @ElementCollection(fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SELECT)
    @MapKeyColumn(name = "key", length = 1024)
    @Column(name = "value", length = 4069)
    private Map<String,String> attributes = new TreeMap<String,String>();


Setting the FetchMode to SELECT causes Hibernate to do a select to the attributes table for every result object it returns. So there is no join and no duplicate results.

Still wondering why the default mode is to use a join, I can't imagine any use for that.

Regards,

Sven


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.