I've had a look to see if anyone has came across this problem before and indeed they have, it seems everyones problem was fixed with the addition of cascade="save-update" though, this is not the case with mine.
Thanks in advance for any help :D
Hibernate version:3.2.6
Mapping documents:
Code:
<hibernate-mapping>
<class name="Event" table="EVENTS">
<id name="id" column="EVENT_ID" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
<set name="clients" table="CLIENTS" cascade="save-update">
<key column="id" />
<one-to-many class="Client" />
</set>
<property name="requiredResources" type="ResourcesUserType" column="resourceNames" />
</class>
<class name="Client" table="Client">
<id name="id" column="CLIENT_ID" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="name"/>
</class>
<class name="Resource" table="Resource">
<id name="id" column="RESOURCE_ID" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Code:
private void createAndStoreEvent(String title, Date theDate, List<Client> clients, List<Resource> resources) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
theEvent.setClients(clients);
theEvent.setRequiredResources(resources);
session.save(theEvent);
session.getTransaction().commit();
}
Full stack trace of any exception that occurs:
Code:
578 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Client
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:101)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:777)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1165)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
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:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at EventManager.createAndStoreEvent(EventManager.java:61)
at EventManager.main(EventManager.java:29)
Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Client
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:101)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:777)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1165)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
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:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at EventManager.createAndStoreEvent(EventManager.java:61)
at EventManager.main(EventManager.java:29)
Name and version of the database you are using:PostGresThe generated SQL (show_sql=true):
Code:
Hibernate: insert into EVENTS (EVENT_ID, EVENT_DATE, title, resourceNames) values (null, ?, ?, ?)
org.hsqldb.jdbc.jdbcPreparedStatement@adb1d4[sql=[insert into EVENTS (EVENT_ID, EVENT_DATE, title, resourceNames) values (null, ?, ?, ?)], parameters=[[2009-02-20 10:36:15.587], [StoreTest], [null]]]
Hibernate: call identity()
Hibernate: update Client set id=? where CLIENT_ID=?
[/code]