TransientObjectException thrown from IContentHandler ???

Topics: Customizing Orchard, Troubleshooting, Writing modules
Jan 6, 2014 at 1:07 PM
Edited Jan 6, 2014 at 1:51 PM
This code is generating the following exception, I must have missed something and still don't understand why, any help welcome.
Seems there is a pb with cascading links in NHibernate ?

I have 2 content items: XXX and XXXTracking, they respectivelly contain XXXPart and XXXTrackingPart, and XXXTrackingPart has a 1:n relation to XXX (the foreign key is made in migration as 1 XXX -> n XXXTracking.)
    public class XXXTrackingPartHandler : ContentHandler 
    {
        private readonly IXXXTrackingService _XXXTrackingService;
        private readonly IClock _clock;

        public XXXTrackingPartHandler(IRepository<XXXTrackingPartRecord> repository
                    , IClock clock
                    , IXXXTrackingService xxxTrackingService)
        {
            _XXXTrackingService   = xxxTrackingService;
            _clock                      = clock;
            Filters.Add(StorageFilter.For(repository));
            OnCreated<XXXPart>(CreateTracking);
            OnUpdated<XXXPart>(CreateTracking);
        }

        XXXTrackingPart CreateTracking(CreateContentContext context,XXXPart part)
        {      
IContentManager contentManager=context.ContentManager;    
            var item                    = contentManager.New("XXXTracking");
            TitlePart Title             = item.As<TitlePart>();
            Title.Title                 = string.Format("{0} on {1:u}",op,_clock.UtcNow);
            XXXTrackingPart track = item.As<XXXTrackingPart>();
            //track.Operation             = op;
            track.XXX             = part.Record;
            track.XmlContent            = _XXXTrackingService.ToXml(part.ContentItem);
            contentManager.Create(item);
            return track;
        }
Orchard.ContentManagement.DefaultContentManager - Default - TransientObjectException thrown from IContentHandler by Datwendo.Connectors.Handlers.XXXTrackingPartHandler
NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave. Type: Datwendo.XXXs.Models.XXXPartRecord, Entity: Datwendo.XXXs.Models.XXXPartRecord
at NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Engine\ForeignKeys.cs:line 264
at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session) in c:\Users\sebros\Documents\My Projects\nhibernate-core\src\NHibernate\Type\EntityType.cs:line 166
Jan 6, 2014 at 2:41 PM
Edited Jan 6, 2014 at 2:47 PM
Found my problem: the OnUpdated<XXXPart>(CreateTracking) is triggered before the XXXPart is created (through Edit/EditPost).
The XXXPart is not even attached to a contentItem ???
If I create an XXXTracking at that time, I get the error because the XXXPart has never been saved.

Looks like an NHibernate bug ?

Now my Pb is how to clean the DB ???