Hi All,
Code:
<class name="com.hibernate.list.TestMap" table="TEST_CLASS_TBL" lazy="false">
<id name="strId" type="string" column="ID">
<generator class="uuid.hex"/>
</id>
<map name="mapCust" table="TEST_MAP_CUST" cascade="all" lazy="false">
<key column="strId"/>
<index-many-to-many class="com.hibernate.list.TestList" column="testList_id" />
<many-to-many column="test_list" class="com.hibernate.list.TestList" />
</map>
</class>
<class name="com.hibernate.list.TestList" table="TEST_LIST_TBL" lazy="false">
<id name="strListId" type="string" column="listID">
<generator class="uuid.hex"/>
</id>
<property name="name" type="string" column="name" />
</class>
And while setting TestList object in map I want clone of same object
mapCust.put((TestList)objTestList.clone(), objTestList);
Also I have override clone method in TestList
Code:
public Object clone() {
TestList objTestList = new TestList();
if(name != null) {
objTestList.setName(new String(name));
}
if(strListId != null) {
objTestList.setStrListId(new String(strListId));
}
return objTestList;
}
when I am trying to save this object it gives me exception
Code:
Hibernate: insert into TEST_CLASS_TBL (ID) values (?)
Hibernate: insert into TEST_LIST_TBL (name, listID) values (?, ?)
Hibernate: insert into TEST_MAP_CUST (strId, testList_id, test_list) values (?, ?, ?)
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.hibernate.list.TestList
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeIndex(AbstractCollectionPersister.java:765)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1141)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:39)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
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.hibernate.list.CollectionExample.main(CollectionExample.java:72)
java.lang.NullPointerException
at com.hibernate.list.CollectionExample.main(CollectionExample.java:171)
Hibernate: insert into TEST_MAP_CUST (strId, testList_id, test_list) values (?, ?, ?)
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.hibernate.list.TestList
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeIndex(AbstractCollectionPersister.java:765)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1141)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:39)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
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 com.hibernate.list.CollectionExample.main(CollectionExample.java:174)
But when I am trying to save same object without cloning it, then it's saving properly.
Please let me know where I am doing wrong?
Is there any problem with cloned object?
Thank You,
Navnath.