I'm sorry to be a bother, since I already asked a similar question here
http://forum.hibernate.org/viewtopic.php?t=981118&highlight=collectionofelements, but I haven't solved that problem yet.
I noticed that I can map a HashMap to the MySQL database with Varchar(255) values if I use this mapping:
Code:
@CollectionOfElements
public Map<String, String> getSettings() {
return settings;
}
The table will look like
Code:
+--------------+--------------+
| Field | Type |
+--------------+--------------+
| Collector_id | bigint(20) |
| element | varchar(255) |
| mapkey | varchar(255) |
+--------------+--------------+
But I want to store more that 255 chars - at least for the value (I already noticed clobs aren't allowed as indices). So I tried
Code:
@CollectionOfElements
@AttributeOverride(name="element.value", column=@Column(columnDefinition="TEXT"))
Which crashed horribly, resulting in
Code:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [C:\Users\tag\workspace\xyz\conf\applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.PropertyNotFoundException: Could not find a setter for property bytes in class java.lang.String
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property bytes in class java.lang.String
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:216)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:209)
at org.hibernate.mapping.Property.getSetter(Property.java:277)
at org.hibernate.tuple.component.PojoComponentTuplizer.buildSetter(PojoComponentTuplizer.java:137)
at org.hibernate.tuple.component.AbstractComponentTuplizer.<init>(AbstractComponentTuplizer.java:44)
at org.hibernate.tuple.component.PojoComponentTuplizer.<init>(PojoComponentTuplizer.java:38)
at org.hibernate.tuple.component.ComponentEntityModeToTuplizerMapping.<init>(ComponentEntityModeToTuplizerMapping.java:52)
at org.hibernate.tuple.component.ComponentMetamodel.<init>(ComponentMetamodel.java:50)
at org.hibernate.mapping.Component.buildType(Component.java:152)
at org.hibernate.mapping.Component.getType(Component.java:145)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:253)
at org.hibernate.mapping.Collection.validate(Collection.java:278)
at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:67)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1106)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1287)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
Also setting the @Column(length=4096) didn't work and resulted in the same exception.
The only thing that worked was getting rid of the annotations and using xml. Like this:
Code:
<map name="settings" >
<key column="id" />
<index column="keyValue" type="string" length="255"/>
<element column="value" type="string" length="1024" />
</map>
which got me an acceptable table:
Code:
mysql> desc testentity_settings;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id | bigint(20) | NO | PRI | | |
| value | text | YES | | NULL | |
| keyValue | varchar(255) | NO | PRI | | |
+----------+--------------+------+-----+---------+-------+
But using XML is out of question due to the nature of the project I am working on. So the question is: How can I get sane Hibernate annotation mappings for what I am doing, without it blowing up in my face everytime I try to do map a pojo.
The Hibernate Manual is very sketchy on this subject, especially when dealing with Maps. I read the Annotation Manual, The Hibernate documentation and the relevant portions of Persistence with Hibernate and came up with basically nothing.
btw. I am using hibernate 3.2.5ga and annotations 3.3.0ga