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.