Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF "Technology" (Ecore Tools, EMFatic, etc)  » [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance
[Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #698694] Wed, 20 July 2011 01:55 Go to next message
Mike Cooper is currently offline Mike CooperFriend
Messages: 46
Registered: February 2011
Member

I'm getting a org.hibernate.TransientObjectException exception when persisting data using Texo generated Hibernate classes:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: xxx.model.Agent
  #0: org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
  #1: org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
  #2: org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
  #3: org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
  #4: org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
  #5: org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3403)
  #6: org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
  #7: org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
  #8: org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
  #9: org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
  #10: org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
  #11: org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
  #12: org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
  #13: org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
  #14: org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
  #15: xxx.db.ModelManagerPersist.endTransaction(ModelManagerPersist.java:103)
  #16: xxx.asset.AssetManagerPersist.save(AssetManagerPersist.java:86)


This exception occurs when doing an endTransaction of a class called xxx.model.Asset which has many-to-one EReference to xxx.model.Agent. I have numerous other similar references in the Asset EClass which likely will have a similar exception problem, but this is the first one which hibernate hits.

I think this is a cascade issue, but the orm.xml generated by Texo seems to set cascade options:

      <orm:many-to-one name="agent" optional="true" target-entity="xxx.model.Agent">
        <orm:join-column/>
        <orm:cascade>
          <orm:cascade-persist/>
          <orm:cascade-merge/>
          <orm:cascade-refresh/>
        </orm:cascade>
      </orm:many-to-one>


All the transaction code worked fine using Teneo. I'm guessing it was doing a lot more heavy lifting under the covers than I knew.

Do I need to manually code a transaction to save each EReference EClass prior to saving the parent EClass?

Re: [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #698848 is a reply to message #698694] Wed, 20 July 2011 11:16 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Hmm, strange with these cascade settings this should not happen. What you can do is get the hibernate source and put a
breakpoint in hibernate to detect the refering object to check the orm.xml in more detail.

gr. Martin

On 07/20/2011 03:55 AM, Mike Cooper wrote:
>
> I'm getting a org.hibernate.TransientObjectException exception when persisting data using Texo generated Hibernate classes:
>
>
> org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance
> before flushing: xxx.model.Agent
> #0: org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)
> #1: org.hibernate.type.EntityType.getIdentifier(EntityType.java:456)
> #2: org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:265)
> #3: org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:275)
> #4: org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:295)
> #5: org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3403)
> #6: org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
> #7: org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
> #8: org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
> #9: org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
> #10:
> org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
> #11: org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
> #12: org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
> #13: org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
> #14: org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
> #15: xxx.db.ModelManagerPersist.endTransaction(ModelManagerPersist.java:103)
> #16: xxx.asset.AssetManagerPersist.save(AssetManagerPersist.java:86)
>
>
> This exception occurs when doing an endTransaction of a class called xxx.model.Asset which has many-to-one EReference to
> xxx.model.Agent. I have numerous other similar references in the Asset EClass which likely will have a similar exception
> problem, but this is the first one which hibernate hits.
>
> I think this is a cascade issue, but the orm.xml generated by Texo seems to set cascade options:
>
>
> <orm:many-to-one name="agent" optional="true" target-entity="xxx.model.Agent">
> <orm:join-column/>
> <orm:cascade>
> <orm:cascade-persist/>
> <orm:cascade-merge/>
> <orm:cascade-refresh/>
> </orm:cascade>
> </orm:many-to-one>
>
>
> All the transaction code worked fine using Teneo. I'm guessing it was doing a lot more heavy lifting under the covers
> than I knew.
>
> Do I need to manually code a transaction to save each EReference EClass prior to saving the parent EClass?
>
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #699137 is a reply to message #698848] Wed, 20 July 2011 22:52 Go to previous messageGo to next message
Mike Cooper is currently offline Mike CooperFriend
Messages: 46
Registered: February 2011
Member
Hi Martin,

Thanks for the suggestion. Unfortunately I'm not clear on what you are suggesting. Where in hibernate should I be set breakpoints? I've never had the pleasure of working at the source level of hibernate.

I'm stuck on this one so it's kinda important to resolve for me. Appreciate all your help!

mike
Re: [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #699271 is a reply to message #699137] Thu, 21 July 2011 08:16 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Hi Mike,
The stack trace shows where the exception is thrown:
#0: org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:243)

So ForeignKeys class line 243.

The hibernate source is contained in the zip/tar.gz where you can download it.

gr. Martin


On 07/21/2011 12:52 AM, Mike Cooper wrote:
> Hi Martin,
>
> Thanks for the suggestion. Unfortunately I'm not clear on what you are suggesting. Where in hibernate should I be set
> breakpoints? I've never had the pleasure of working at the source level of hibernate.
>
> I'm stuck on this one so it's kinda important to resolve for me. Appreciate all your help!
>
> mike
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #699592 is a reply to message #699271] Thu, 21 July 2011 21:18 Go to previous messageGo to next message
Mike Cooper is currently offline Mike CooperFriend
Messages: 46
Registered: February 2011
Member
Thanks Martin. I've traced the problem down to this:

Class: AbstractEntityTuplizer

	public Serializable getIdentifier(Object entity, SessionImplementor session) {
		final Object id;
		if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {
			id = entity;
		}
		else {
			if ( idGetter == null ) {
				if (identifierMapperType==null) {
					throw new HibernateException( "The class has no identifier property: " + getEntityName() );
				}
				else {
					id = mappedIdentifierValueMarshaller.getIdentifier( entity, getEntityMode(), session );
				}
			}
			else {
                id = idGetter.get( entity );
            }
        }

		try {
/* RETURNS null => */	return (Serializable) id;
		}


The "id" var is returned as null. Back up the stack this causes hibernate to mark the class as transient.

I'm definitely out of my depth at this point and not sure how to proceed. Do you think this is related to the texo generated orm.xml or some other texo generated file? Or is this likely to be something in my code or hibernate itself?

What's a good reference doc for the orm.xml syntax? Maybe I can dig around there.

mike
Re: [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #699671 is a reply to message #699592] Fri, 22 July 2011 04:12 Go to previous messageGo to next message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Hi Mike,
I don't think it is a specific Texo problem (cascades have different meanings and there are pros/cons to different
settings in different situations). But maybe the mapping can be generated in such a way that this problem does not occur
directly, although afaics the orm.xml is fine.

Is this something I can easily try myself? If you can send me a zipped project directly by email with a testcase I can
run then I can try to reproduce this.

There is lots of info on jpa/orm on the internet. The spec is quite readable, specifically the part where the
annotations are explained, chapters 11 and 12 (see attached):
http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html

gr. Martin

On 07/21/2011 11:18 PM, Mike Cooper wrote:
> Thanks Martin. I've traced the problem down to this:
>
> Class: AbstractEntityTuplizer
>
>
> public Serializable getIdentifier(Object entity, SessionImplementor session) {
> final Object id;
> if ( entityMetamodel.getIdentifierProperty().isEmbedded() ) {
> id = entity;
> }
> else {
> if ( idGetter == null ) {
> if (identifierMapperType==null) {
> throw new HibernateException( "The class has no identifier property: " + getEntityName() );
> }
> else {
> id = mappedIdentifierValueMarshaller.getIdentifier( entity, getEntityMode(), session );
> }
> }
> else {
> id = idGetter.get( entity );
> }
> }
>
> try {
> /* RETURNS null => */ return (Serializable) id;
> }
>
>
> The "id" var is returned as null. Back up the stack this causes hibernate to mark the class as transient.
>
> I'm definitely out of my depth at this point and not sure how to proceed. Do you think this is related to the texo
> generated orm.xml or some other texo generated file? Or is this likely to be something in my code or hibernate itself?
> What's a good reference doc for the orm.xml syntax? Maybe I can dig around there.
>
> mike
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Re: [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #700032 is a reply to message #699671] Fri, 22 July 2011 18:29 Go to previous messageGo to next message
Mike Cooper is currently offline Mike CooperFriend
Messages: 46
Registered: February 2011
Member
I have it working now. If I manually edit orm.xml and replace all of the <orm:cascade-.../> entries with <orm:cascade-all/> it works fine. Looking back at how my code used Teneo prior to Texo I also set "cascade=all" so it makes sense now.

So how do I get Texo to use cascade-all instead of cascade-merge, etc? Should I file an enhencement/bug in bugzilla?

Cheers,
mike
Re: [Texo/Hibernate] TransientObjectException: object references an unsaved transient instance [message #700119 is a reply to message #700032] Fri, 22 July 2011 20:42 Go to previous message
Martin Taal is currently offline Martin TaalFriend
Messages: 5340
Registered: July 2009
Senior Member
Yes please do enter a bugzilla, I am surprised as persist, merge and refresh are the most common one, the other ones
(remove, refresh, detach) make less sense.If you enter a bugzilla then I will add refresh and detach, remove just does
not make sense for most many-to-ones...

gr. Martin

On 07/22/2011 08:29 PM, Mike Cooper wrote:
> I have it working now. If I manually edit orm.xml and replace all of the <orm:cascade-.../> entries with
> <orm:cascade-all/> it works fine. Looking back at how my code used Teneo prior to Texo I also set "cascade=all" so it
> makes sense now.
>
> So how do I get Texo to use cascade-all instead of cascade-merge, etc? Should I file an enhencement/bug in bugzilla?
>
> Cheers,
> mike
>


--

With Regards, Martin Taal

Springsite/Elver.org
Office: Hardwareweg 4, 3821 BV Amersfoort
Postal: Nassaulaan 7, 3941 EC Doorn
The Netherlands
Cell: +31 (0)6 288 48 943
Tel: +31 (0)84 420 2397
Fax: +31 (0)84 225 9307
Mail: mtaal@springsite.com - mtaal@elver.org
Web: www.springsite.com - www.elver.org
Previous Topic:[EMFFacet] Profile/Stereotypes for EMF
Next Topic:[EMFCompare] Comparison of unordered lists
Goto Forum:
  


Current Time: Sun Dec 21 01:54:54 GMT 2014

Powered by FUDForum. Page generated in 0.02026 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software