Hi,
I am relative newbie to hibernate. I am getting a org.hibernate.TransientObjectException error when I delete a child object "scenario" and then ask the parent for a list of scenarios. I am able to recover fine and subsequently fetch child "scenario" objects. It just the sequence "delete child" "fetch children" that fails. Everything works fine if I don't lazy load the parent from the child. I don't want to have the fetch the parent separately. I tried some of the cascade options but they did not seem to work.
Any help would be greatly appreciated.
Steve
error:
SEVERE: findAll
org.hibernate.TransientObjectException: object references an unsaved transient instance -
save the transient instance before flushing: com.loadgeneral.model.Workspace
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:242)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:430)
java code
Code:
// this fails if the parent object workspace is not lazy loaded.
// This works if the the parent "workspace" is lazy loaded in the child object "scenario"
// delete(childScenario);
// findAll(workspace) ----> this throws org.hibernate.TransientObjectException
public List findAll(Workspace workspace) {
log.debug("finding all");
try {
begin();
Query q = getSession()
.createQuery("from com.loadgeneral.model.Scenario s where s.instance = :instance and s.workspace = :workspace order by s.lastmodified desc");
q.setParameter("instance", 0);
q.setParameter("workspace", workspace);
List results = q.list();
commit();
log.debug("find all successful, result size: " + results.size());
return results;
}
catch (RuntimeException re) {
log.error("findAll", re);
throw re;
}
finally{
close();
}
}
// delete child object, this works fine
public void delete(Integer id) {
log.debug("delete Object instance");
try {
begin();
Object instance = getSession()
.get(this.persistentClass, id);
getSession().delete(instance);
commit();
log.debug("delete successful");
}
catch (RuntimeException re) {
rollback();
log.error("delete failed", re);
throw re;
}
finally{
close();
}
}
Mappings
Code:
<hibernate-mapping>
<class name="com.loadgeneral.model.Workspace" table="WORKSPACE">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
....
<!-- children class -->
<set name="scenarios" inverse="true" lazy="true" table="SCENARIO" fetch="select">
<key>
<column name="WORKSPACE_ID" not-null="true" />
</key>
<one-to-many class="com.loadgeneral.model.Scenario" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="com.loadgeneral.model.Scenario" table="SCENARIO" select-before-update="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<!-- parent class -->
<many-to-one name="workspace" class="com.loadgeneral.model.Workspace" update="false" lazy="false" fetch="select">
<column name="WORKSPACE_ID" not-null="true" />
</many-to-one>
..
</hibernate-mapping>