Hi everyone,
I am having troubles with mapping a map containing an array as it's value type (a simple array of string).
The only piece of information relating to my problem I could find was the following:
http://osdir.com/ml/java.french.general ... 00025.html
It is in french but the person answering mainly explains that his understanding of the DTD makes him think that it's impossible to persist a map of map.
Is it true or is there a piece of information I missed in the documentation?
I don't know what to put in place of the <element> tag to be able to persist that array of string.
Code:
<map name="mMap">
<key column="MyMapId"/>
<map-key type="string"/>
<element type="string"/>
</map>
Hibernate version:
3.2.2
Hibernate configuration:
<!-- Auto commit transactions to the DB -->
<property name="connection.autocommit">false</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">auto</property>
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- TODO find a better solution for lazy loading policy -->
<hibernate-mapping package="com.tests"
default-access="field"
default-cascade="save-update"
default-lazy="false">
<class name="TestMap$MyMap" table="mymap">
<id type="long">
<generator class="increment"/>
</id>
<map name="mMap">
<key column="MyMapId"/>
<map-key type="string"/>
<element type="string"/> <!-- there I would like to put something like type=string-array or use an <array> instead of the <element> tag-->
</map>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Hibernate: select max(id) from mymap
Hibernate: insert into mymap (id) values (?)
Hibernate: insert into mMap (MyMapId, idx, elt) values (?, ?, ?)
java.lang.ClassCastException
at org.hibernate.type.StringType.toString(StringType.java:44)
at org.hibernate.type.NullableType.nullSafeToString(NullableType.java:93)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:140)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:107)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.tests.TestMap.testMap(TestMap.java:62)
at com.tests.TestMap.main(TestMap.java:47)
The class cast exception obviously happens because the java type is String[] and mapping file describes says it's a string since I don't know how to specify it should be an array.