-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Cascade options and composite object graphs
PostPosted: Wed May 18, 2005 1:06 am 
Regular
Regular

Joined: Fri Oct 01, 2004 2:19 am
Posts: 111
Location: Melbourne, Australia
Hibernate version: 3.0.3

Mapping documents:
the following is a model fragment to demonstrate my problem:
Code:
class ModelObject {}
class MO_A extends ModelObject {}
class MO_B extends ModelObject { MO_A ref1, ref2;}

class Cmd
{
   ModelObject source;
   Project proj;
   Cmd parent; //cascade: none
   List<Cmd> children; //cascade: all, delete-orphan
}
class Project
{
    List<Cmd> commands; //cascade: all, delete-orphan
}


The following instance diagram depicts the makeup of the object graph:
Code:
p: Project
  *-------+ MO_B_Cmd : Cmd  <+
                      |      |
                      + MO_A_Cmd : Cmd  *---> mo_a : MO_A





The associated scenario is that a new MO_B needs to be created.
This is achieved by adding a new Cmd derived class which knows how
to construct a MO_B. So far nothing special. In order to construct an
MO_B, a lookup is performed to see if the ref1/ref2 objects already exist
in the database. If they're not, then they get created using their
respective Cmd classes. When such an event occurs, the new Cmd
instance is added to the parent Cmd.

In the case under test, ref1 (the first MO_A to be looked up) needs to be
instantiated and the appropriate Cmd instance together with the new
MO_A (the type of ref1) are added to the parent Cmd.

However, when ref2 is attempted to be looked up, Hibernate performs an
autoflush and throws the exception below. This is happening when it
attempts to perform a flush on the Project instance p which is comprised
of the objects as shown above.
When Hibernate gets to the MO_A_Cmd instance, is detects that
MO_B_Cmd is also transient and throws the exception.



Full stack trace of any exception that occurs:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: beans.Cmd
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:216)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:99)
at org.hibernate.type.EntityType.isDirty(EntityType.java:216)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:412)
at org.hibernate.persister.entity.BasicEntityPersister.findDirty(BasicEntityPersister.java:2528)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:343)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:108)
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.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:39)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:711)
at org.hibernate.impl.SessionImpl.prepareQueries(SessionImpl.java:895)
at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:885)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)


Debug level Hibernate log excerpt:
14:26:18,031 DEBUG main org.hibernate.hql.ast.QueryTranslatorImpl - compile() : The query is already compiled, skipping...
14:26:18,031 DEBUG main org.hibernate.event.def.AbstractFlushingEventListener - flushing session
14:26:18,031 DEBUG main org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades
14:26:18,046 DEBUG main org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: *** other application objects
14:26:18,046 DEBUG main org.hibernate.engine.Cascades - processing cascade ACTION_SAVE_UPDATE for: <application>.model.beans.Project
14:26:18,046 DEBUG main org.hibernate.engine.Cascades - cascade ACTION_SAVE_UPDATE for collection: <application>.model.beans.Project.commands
14:26:18,046 DEBUG main org.hibernate.engine.Cascades - cascading to saveOrUpdate: <application>.command.beans.Cmd
14:26:18,046 DEBUG main org.hibernate.engine.Cascades - id unsaved-value: -1
14:26:18,046 DEBUG main org.hibernate.event.def.AbstractSaveEventListener - transient instance of: <application>.command.beans.Cmd
14:26:18,046 DEBUG main org.hibernate.event.def.DefaultSaveOrUpdateEventListener - saving transient instance
14:26:18,046 DEBUG main org.hibernate.jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
14:26:18,046 DEBUG main org.hibernate.SQL - select hibernate_sequence.nextval from dual
14:26:18,046 DEBUG main org.hibernate.jdbc.AbstractBatcher - preparing statement
14:26:18,062 DEBUG main org.hibernate.id.SequenceGenerator - Sequence identifier generated: 3051
14:26:18,062 DEBUG main org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
14:26:18,062 DEBUG main org.hibernate.jdbc.AbstractBatcher - closing statement
14:26:18,062 DEBUG main org.hibernate.event.def.AbstractSaveEventListener - generated identifier: 3051, using strategy: org.hibernate.id.SequenceGenerator
14:26:18,062 DEBUG main org.hibernate.event.def.AbstractSaveEventListener - saving [<application>.command.beans.Cmd#3051]
14:26:18,062 DEBUG main org.hibernate.engine.Cascades - processing cascade ACTION_SAVE_UPDATE for: <application>.command.beans.Cmd
14:26:18,062 DEBUG main org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: <application>.command.beans.Cmd
14:26:18,062 DEBUG main org.hibernate.event.def.WrapVisitor - Wrapped collection in role: <application>.command.beans.Cmd.sources
14:26:18,062 DEBUG main org.hibernate.event.def.WrapVisitor - Wrapped collection in role: <application>.command.beans.Cmd.commands
14:26:18,062 DEBUG main org.hibernate.engine.Cascades - id unsaved-value: -1
14:26:18,062 DEBUG main org.hibernate.engine.Cascades - processing cascade ACTION_SAVE_UPDATE for: <application>.command.beans.Cmd
14:26:18,062 DEBUG main org.hibernate.engine.Cascades - cascade ACTION_SAVE_UPDATE for collection: <application>.command.beans.Cmd.sources
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - cascading to saveOrUpdate: null
14:26:18,078 DEBUG main org.hibernate.event.def.AbstractSaveEventListener - persistent instance of: <application>.model.beans.VNE
14:26:18,078 DEBUG main org.hibernate.event.def.DefaultSaveOrUpdateEventListener - ignoring persistent instance
14:26:18,078 DEBUG main org.hibernate.event.def.DefaultSaveOrUpdateEventListener - object already associated with session: [<application>.model.beans.VNE#3050]
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: <application>.command.beans.Cmd.sources
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - cascade ACTION_SAVE_UPDATE for collection: <application>.command.beans.Cmd.commands
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: <application>.command.beans.Cmd.commands
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - deleting orphans for collection: <application>.command.beans.Cmd.commands
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - done deleting orphans for collection: <application>.command.beans.Cmd.commands
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: <application>.command.beans.Cmd
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - done cascade ACTION_SAVE_UPDATE for collection: <application>.model.beans.Project.commands
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - deleting orphans for collection: <application>.model.beans.Project.commands
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - done deleting orphans for collection: <application>.model.beans.Project.commands
14:26:18,078 DEBUG main org.hibernate.engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: <application>.model.beans.Project
14:26:18,078 DEBUG main org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections
14:26:18,078 DEBUG main org.hibernate.engine.CollectionEntry - Collection dirty: [<application>.model.beans.Project.commands#3037]
14:26:18,078 DEBUG main org.hibernate.engine.CollectionEntry - Collection dirty: [<application>.model.beans.Project.objectMap#3037]
14:26:18,078 DEBUG main org.hibernate.event.def.AbstractFlushingEventListener - Flushing entities and processing referenced collections
14:26:18,078 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.NE.ports#3038], was: [<application>.model.beans.NE.ports#3038] (uninitialized)
14:26:18,078 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.NE.vnes#3038], was: [<application>.model.beans.NE.vnes#3038] (uninitialized)
14:26:18,078 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.NE.baseAddresses#3038], was: [<application>.model.beans.NE.baseAddresses#3038] (uninitialized)
14:26:18,093 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.NE.ports#3040], was: [<application>.model.beans.NE.ports#3040] (uninitialized)
14:26:18,093 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.NE.vnes#3040], was: [<application>.model.beans.NE.vnes#3040] (uninitialized)
14:26:18,093 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.NE.baseAddresses#3040], was: [<application>.model.beans.NE.baseAddresses#3040] (initialized)
14:26:18,093 DEBUG main org.hibernate.event.def.DefaultFlushEntityEventListener - Updating entity: [<application>.model.beans.Project#3037]
14:26:18,093 DEBUG main org.hibernate.engine.Versioning - Incrementing: 0 to 1
14:26:18,093 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.Project.commands#3037], was: [<application>.model.beans.Project.commands#3037] (uninitialized)
14:26:18,093 DEBUG main org.hibernate.engine.Collections - Collection found: [<application>.model.beans.Project.objectMap#3037], was: [<application>.model.beans.Project.objectMap#3037] (initialized)
14:26:18,093 DEBUG main org.hibernate.engine.Cascades - id unsaved-value: -1






What do I need to do in order to avoid this? I would like to avoid changing
the cascade level, since then persistence operation will no longer be
transparent as they are now.

_________________
Cheers,

Bonny

please don't forget to rate :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.