Keep getting this -
Code:
org.hibernate.HibernateException: identifier of an instance of TestEntity altered from {key1=1, key2=1} to {key1=1, prop1=test, type=TestEntity, key2=1}
...when I haven't touched the Map.
Looks like it's confusing the whole Map with the subset of the Map used for the identifier.
Suggestions / workarounds?
Many thanks
Jeremy.
Using "dynamic-class"
Hibernate version: 3.0rc1Mapping documents:Code:
<hibernate-mapping>
<class entity-name="TestEntity" table="HIBTEST" schema="MARS">
<composite-id>
<key-property name="key1" type="java.lang.Long"/>
<key-property name="key2" type="java.lang.Long"/>
</composite-id>
<property name="prop1" type="java.lang.String">
<column name="col1" sql-type="VARCHAR2"/>
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Configuration cfg = new Configuration();
cfg.setProperty(Environment.DEFAULT_ENTITY_MODE, EntityMode.MAP.toString());
cfg.addFile("classes/test/test.hbm.xml");
cfg.configure();
SessionFactory factory = cfg.buildSessionFactory();
Session s = factory.openSession();
Transaction tx = null;
try
{
// s.setFlushMode(FlushMode.NEVER); //entirely optional!!
tx = s.beginTransaction();
log("Getting a test entity");
Map si = (Map) s.createQuery(
"from TestEntity te where te.key1='1' and te.key2='1' "
).uniqueResult();
/*
for (Iterator attrs = si.keySet().iterator(); attrs.hasNext(); )
{
String attrName = (String) attrs.next();
log(attrName+ " = "+ si.get(attrName));
}
*/
tx.commit();
}
catch (Exception e)
{
if (tx != null) tx.rollback();
throw e;
}
finally
{
s.close();
}
}
Full stack trace of any exception that occurs:Code:
Getting a test entity
Hibernate: select testentity0_.key1 as key1_, testentity0_.key2 as key2_, testentity0_.col1 as col3_0_ from MARS.HIBTEST testentity0_ where (testentity0_.key1='1' and testentity0_.key2='1')
org.hibernate.HibernateException: identifier of an instance of TestEntity altered from {key1=1, key2=1} to {key1=1, prop1=test, type=TestEntity, key2=1}
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at test.HibTestCase.main(HibTestCase.java:54)
Exception in thread "main"
Name and version of the database you are using:Oracle 9.2.0.4
The generated SQL (show_sql=true):Code:
Hibernate: select testentity0_.key1 as key1_, testentity0_.key2 as key2_, testentity0_.col1 as col3_0_ from MARS.HIBTEST testentity0_ where (testentity0_.key1='1' and testentity0_.key2='1')
Debug level Hibernate log excerpt:
10:18:20,767 DEBUG QueryTranslatorImpl:177 - HQL: from TestEntity te where te.key1='1' and te.key2='1'
10:18:20,767 DEBUG QueryTranslatorImpl:178 - SQL: select testentity0_.key1 as key1_, testentity0_.key2 as key2_, testentity0_.col1 as col3_0_ from MARS.HIBTEST testentity0_ where (testentity0_.key1='1' and testentity0_.key2='1')
10:18:20,767 DEBUG AbstractBatcher:258 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
10:18:20,767 DEBUG SQL:292 - select testentity0_.key1 as key1_, testentity0_.key2 as key2_, testentity0_.col1 as col3_0_ from MARS.HIBTEST testentity0_ where (testentity0_.key1='1' and testentity0_.key2='1')
Hibernate: select testentity0_.key1 as key1_, testentity0_.key2 as key2_, testentity0_.col1 as col3_0_ from MARS.HIBTEST testentity0_ where (testentity0_.key1='1' and testentity0_.key2='1')
10:18:20,767 DEBUG AbstractBatcher:343 - preparing statement
10:18:20,798 DEBUG AbstractBatcher:274 - about to open ResultSet (open ResultSets: 0, globally: 0)
10:18:20,798 DEBUG Loader:377 - processing result set
10:18:20,798 DEBUG Loader:382 - result set row: 0
10:18:20,830 DEBUG LongType:86 - returning '1' as column: key1_
10:18:20,830 DEBUG LongType:86 - returning '1' as column: key2_
10:18:20,830 DEBUG Loader:719 - result row: EntityKey[TestEntity#{key1=1, key2=1}]
10:18:20,830 DEBUG Loader:864 - Initializing object from ResultSet: [TestEntity#{key1=1, key2=1}]
10:18:20,830 DEBUG BasicEntityPersister:1625 - Hydrating entity: [TestEntity#{key1=1, key2=1}]
10:18:20,830 DEBUG StringType:86 - returning 'test' as column: col3_0_
10:18:20,845 DEBUG Loader:399 - done processing result set (1 rows)
10:18:20,845 DEBUG AbstractBatcher:281 - about to close ResultSet (open ResultSets: 1, globally: 1)
10:18:20,845 DEBUG AbstractBatcher:266 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
10:18:20,845 DEBUG AbstractBatcher:363 - closing statement
10:18:20,845 DEBUG Loader:450 - total objects hydrated: 1
10:18:20,845 DEBUG TwoPhaseLoad:96 - resolving associations for [TestEntity#{key1=1, key2=1}]
10:18:20,845 DEBUG TwoPhaseLoad:167 - done materializing entity [TestEntity#{key1=1, key2=1}]
10:18:20,845 DEBUG PersistenceContext:738 - initializing non-lazy collections
10:18:20,845 DEBUG JDBCTransaction:83 - commit
10:18:20,845 DEBUG SessionImpl:292 - automatically flushing session
10:18:20,845 DEBUG AbstractFlushingEventListener:52 - flushing session
10:18:20,845 DEBUG AbstractFlushingEventListener:102 - processing flush-time cascades
10:18:20,845 DEBUG AbstractFlushingEventListener:150 - dirty checking collections
10:18:20,845 DEBUG AbstractFlushingEventListener:167 - Flushing entities and processing referenced collections
10:18:20,861 DEBUG JDBCTransaction:124 - rollback
10:18:20,861 DEBUG JDBCContext:208 - before transaction completion
10:18:20,861 DEBUG SessionImpl:337 - before transaction completion
10:18:20,861 DEBUG JDBCTransaction:135 - rolled back JDBC Connection
10:18:20,861 DEBUG JDBCContext:213 - after transaction completion
10:18:20,861 DEBUG SessionImpl:353 - after transaction completion
10:18:20,861 DEBUG SessionImpl:246 - closing session
10:18:20,861 DEBUG AbstractBatcher:394 - closing JDBC connection (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)
10:18:20,861 DEBUG DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1
10:18:20,861 DEBUG JDBCContext:213 - after transaction completion
10:18:20,861 DEBUG SessionImpl:353 - after transaction completion
org.hibernate.HibernateException: identifier of an instance of TestEntity altered from {key1=1, key2=1} to {key1=1, prop1=test, type=TestEntity, key2=1}
at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:51)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:190)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:70)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at test.HibTestCase.main(HibTestCase.java:54)
Exception in thread "main"