-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping problem: HashMap<enum, enum>
PostPosted: Thu Aug 27, 2009 12:18 pm 
Newbie

Joined: Sun Sep 30, 2007 12:59 pm
Posts: 5
Hello everyone,

I have the following problem and was not able to find a solution, so you are my last chance.

I am using Hibernate with annotations to generate my database tables. Everything works fine, except for the property relevance, which is a Map which has a enum as key and enum as value. The value of the Map is mapped correctly to VARCHAR, but the key is driving me crazy. At first I had the following annotations:

Code:
@Entity
public class QualityIndicator implements Serializable {
    ...
    @CollectionOfElements
    @JoinTable(name = "qproperty_weighting")
    @Enumerated(value = EnumType.STRING)
    @MapKey(columns = @Column(name = "qproperty"))
    @Column(name = "relevance")
    private Map<QualityProperty, Weighting> relevance = new HashMap<QualityProperty, Weighting>();
    ...
}

public enum QualityProperty { ... }

public enum Weighting { ... }


When I start tomcat I get the following error:
Code:
ERROR [org.hibernate.tool.hbm2ddl.SchemaUpdate] - <BLOB/TEXT column 'qproperty' used in key specification without a key length>


After a few hours I got to a solution with no errors...

Code:
@Entity
public class QualityIndicator implements Serializable {
    ...
    @CollectionOfElements
    @JoinTable(name = "qproperty_weighting")
    @Enumerated(value = EnumType.STRING)
    @MapKey(columns = @Column(name = "qproperty"),
                  type = @Type(
                      type="org.hibernate.type.EnumType",
                      parameters = {@Parameter(name = "enumClass", value="ooo.xxx.yyy.domain.QualityProperty")}
                  )
    )
    @Column(name = "relevance")
    private Map<QualityProperty, Weighting> relevance = new HashMap<QualityProperty, Weighting>();
    ...
}


The problem here is that the enum QualityProperty is mapped to an INTEGER value (the ordinal value) in the database. But I need it to be mapped to a VARCHAR representation (like it is done with simple enum values when using @Enumerated(value = EnumType.STRING)).

Can anyone tell me how I can tell Hibernate to persist the enum key of the map as a VARCHAR value?

I would be deeply grateful for any help or even small hints.

Regards Dumbi



PS: I am not native english speaker, so please indulge my english.

I am using MySQL 5.0.45, the table engine is set to InnoDB, Apache Tomcat 6.0.20, Java 6, Hibernate 3.3.2 GA and Hibernate Annotations 3.4 GA.


Top
 Profile  
 
 Post subject: Re: Mapping problem: HashMap<enum, enum>
PostPosted: Thu Aug 27, 2009 1:50 pm 
Newbie

Joined: Sun Sep 30, 2007 12:59 pm
Posts: 5
Hi again,

I finally found the solution myself here... http://opensource.atlassian.com/project ... se/ANN-657

the trick is to add...
Code:
@Parameter(name="type", value="12")

...to the type parameters of qproperty (the key of the map). 12 seems to be the value for VARCHAR.

Complete solution:
Code:
@Entity
public class QualityIndicator implements Serializable {
    ...
    @CollectionOfElements
    @JoinTable(name = "qproperty_weighting")
    @Enumerated(value = EnumType.STRING)
    @MapKey(columns = @Column(name = "qproperty"),
                  type = @Type(
                      type="org.hibernate.type.EnumType",
                      parameters = {@Parameter(name = "enumClass", value="ooo.xxx.yyy.domain.QualityProperty"), @Parameter(name="type", value="12")}
                  )
    )
    @Column(name = "relevance")
    private Map<QualityProperty, Weighting> relevance = new HashMap<QualityProperty, Weighting>();
    ...
}


Regards,
dumbi


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