Hello,
I have a problem with entities in entity-mode dynamic-map.
For a legacy database I have to use maps for dynamic entities. So I use entity-mode dynamic-map. Also a table has a foreign-key to a non-primary-key of an other table.
I played with the mapping: loading existing entries and changing them now works good. But if I try to persist a new filled map I get an exception, which I don't understand.
I would be very happy if someone could tell me, what I do wrong.
My mapping looks like this:
Code:
<class entity-name="A" table="TABLE_A">
<id name="A_ID" type="long">
<generator class="sequence">
<param name="sequence">TABLE_A_SEQ</param>
</generator>
</id>
<property name="A_SECOND_ID" type="integer" not-null="true" />
<properties name="ATTRIBUTESKEY" update="false" unique="true">
<property name="A_ID" column="A_ID" type="long" update="false" insert="false" />
<property name="A_SECOND_ID" type="integer" update="false" insert="false" />
</properties>
<property name="A_STATUS" type="string" />
<map name="ATTRIBUTES" cascade="all" lazy="false">
<key property-ref="ATTRIBUTESKEY" not-null="true" unique="true" update="false">
<column name="ATTRIBUTE_A_ID" />
<column name="ATTRIBUTE_SECOND_ID" />
</key>
<map-key column="ATTRIBUTE_NAME" type="string" />
<one-to-many entity-name="ATTRIBUTE" />
</map>
</class>
<class entity-name="ATTRIBUTE" table="TABLE_ATTRIBUTE" lazy="false">
<id name="ATTRIBUTE_ID" type="long">
<generator class="sequence">
<param name="sequence">TABLE_ATTRIBUTE_SEQ</param>
</generator>
</id>
<property name="ATTRIBUTE_SECOND_ID" type="integer" insert="false" update="false"/>
<property name="ATTRIBUTE_NAME" type="string" insert="false" update="false" />
<property name="ATTRIBUTE_STRING" type="string" />
</class>
My test-code to persist a new A-entity:
Code:
[...]
Map a = new HashMap();
a.put("A_SECOND_ID", 1);
a.put("A_STATUS", "1");
// why do I have to do this?
a.put("ATTRIBUTESKEY", a);
session.persist("A", a);
Map attributes = new HashMap();
a.put("ATTRIBUTES", attributes);
Map attribute = new HashMap();
attribute.put("ATTRIBUTE_STRWERT", "value");
attribute.put("ATTRIBUTE_NAME", "test");
attributes.put("test", attribute);
tx.commit();
[...]
the thrown exception:
Code:
java.lang.ClassCastException: java.lang.Long
at org.hibernate.property.MapAccessor$MapGetter.get(MapAccessor.java:67)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:64)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:70)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:353)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:348)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:77)
at org.hibernate.engine.ForeignKeys$Nullifier.nullifyTransientReferences(ForeignKeys.java:47)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:94)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:131)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:122)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:65)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
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)
Greetings,
Mark