Thanks for the suggestion to remove the inverse attribute.
I have removed the inverse attribute. No joy.
Code:
Parent p = new Parent();
session.Save(p);
Child c = new Child();
p.AddChild(c);
session.Save(p);
session.Flush();
The child object is still not saved to the database.
I am now getting an exception when I call session.Flush.
object references an unsaved transient instance - save the transient instance before flushing: NHRepro.Child.
Here is the new log4net output:
1028 [3340] INFO NHibernate.Cfg.Environment (null) - NHibernate 1.0.2
1885 [3340] INFO NHibernate.Cfg.Environment (null) - Using reflection optimizer
1885 [3340] INFO NHibernate.Cfg.Configuration (null) - Searching for mapped documents in assembly: NHRepro
1901 [3340] INFO NHibernate.Cfg.Configuration (null) - Found mapping documents in assembly: NHRepro.Child.hbm.xml
2041 [3340] INFO NHibernate.Dialect.Dialect (null) - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2088 [3340] INFO NHibernate.Cfg.Binder (null) - Mapping class: NHRepro.Child -> [Child]
2104 [3340] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: ChildID -> child_id, type: Int32
2119 [3340] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: Parent -> myparent_id, type: Parent
2119 [3340] INFO NHibernate.Cfg.Configuration (null) - Found mapping documents in assembly: NHRepro.Parent.hbm.xml
2119 [3340] INFO NHibernate.Dialect.Dialect (null) - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2119 [3340] INFO NHibernate.Cfg.Binder (null) - Mapping class: NHRepro.Parent -> [Parent]
2119 [3340] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: ParentID -> parent_id, type: Int32
2135 [3340] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: Children, type: ICollection
2135 [3340] INFO NHibernate.Cfg.Configuration (null) - processing one-to-many association mappings
2135 [3340] DEBUG NHibernate.Cfg.Binder (null) - Second pass for collection: NHRepro.Parent.Children
2135 [3340] INFO NHibernate.Cfg.Binder (null) - mapping collection: NHRepro.Parent.Children -> [Child]
2135 [3340] DEBUG NHibernate.Cfg.Binder (null) - Mapped collection key: parent_id, one-to-many: Child
2135 [3340] INFO NHibernate.Cfg.Configuration (null) - processing one-to-one association property references
2135 [3340] INFO NHibernate.Cfg.Configuration (null) - processing foreign key constraints
2135 [3340] DEBUG NHibernate.Cfg.Configuration (null) - resolving reference to class: Parent
2150 [3340] DEBUG NHibernate.Cfg.Configuration (null) - resolving reference to class: Parent
2150 [3340] INFO NHibernate.Dialect.Dialect (null) - Using dialect: NHibernate.Dialect.MsSql2000Dialect
2150 [3340] INFO NHibernate.Cfg.SettingsFactory (null) - use outer join fetching: True
2150 [3340] INFO NHibernate.Connection.ConnectionProviderFactory (null) - Intitializing connection provider: NHibernate.Connection.DriverConnectionProvider
2150 [3340] INFO NHibernate.Connection.ConnectionProvider (null) - Configuring ConnectionProvider
2150 [3340] INFO NHibernate.Cfg.SettingsFactory (null) - Optimize cache for minimal puts: False
2150 [3340] INFO NHibernate.Cfg.SettingsFactory (null) - echoing all SQL to stdout
2150 [3340] INFO NHibernate.Cfg.SettingsFactory (null) - Query language substitutions: {}
2150 [3340] INFO NHibernate.Cfg.SettingsFactory (null) - cache provider: NHibernate.Cache.HashtableCacheProvider
2150 [3340] INFO NHibernate.Cfg.Configuration (null) - instantiating and configuring caches
2166 [3340] INFO NHibernate.Impl.SessionFactoryImpl (null) - building session factory
2166 [3340] DEBUG NHibernate.Impl.SessionFactoryImpl (null) - instantiating session factory with properties: {hibernate.dialect=NHibernate.Dialect.MsSql2000Dialect, hibernate.use_reflection_optimizer=True, hibernate.connection.connection_string=Server=localhost;initial catalog=NHRepro;User ID=OneThroughSix; Password=123456, hibernate.connection.provider=NHibernate.Connection.DriverConnectionProvider, hibernate.connection.driver_class=NHibernate.Driver.SqlClientDriver, hibernate.show_sql=true}
2213 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Init compiler for class NHRepro.Child
2213 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\nhibernate.dll
2213 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly C:\brennan\NHRepro\bin\Debug\NHRepro.exe
2213 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll
2213 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\log4net.dll
2213 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll
3725 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Compiled ok:
using System;
using NHibernate.Property;
namespace NHibernate.Persister {
public class GetSetHelper_NHRepro_Child : IGetSetHelper {
ISetter[] setters;
IGetter[] getters;
public GetSetHelper_NHRepro_Child(ISetter[] setters, IGetter[] getters) {
this.setters = setters;
this.getters = getters;
}
public void SetPropertyValues(object obj, object[] values) {
NHRepro.Child t = (NHRepro.Child)obj;
try
{
t.Parent = (NHRepro.Parent)values[0];
}
catch( InvalidCastException ice )
{
throw new MappingException(
"Invalid mapping information specified for type " + obj.GetType() + ", check your mapping file for property type mismatches",
ice);
}
}
public object[] GetPropertyValues(object obj) {
NHRepro.Child t = (NHRepro.Child)obj;
object[] ret = new object[1];
ret[0] = t.Parent;
return ret;
}
}
}
3740 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Init compiler for class NHRepro.Parent
3740 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\nhibernate.dll
3740 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly C:\brennan\NHRepro\bin\Debug\NHRepro.exe
3740 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll
3740 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\log4net.dll
3740 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll
3880 [3340] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Compiled ok:
using System;
using NHibernate.Property;
namespace NHibernate.Persister {
public class GetSetHelper_NHRepro_Parent : IGetSetHelper {
ISetter[] setters;
IGetter[] getters;
public GetSetHelper_NHRepro_Parent(ISetter[] setters, IGetter[] getters) {
this.setters = setters;
this.getters = getters;
}
public void SetPropertyValues(object obj, object[] values) {
NHRepro.Parent t = (NHRepro.Parent)obj;
try
{
t.Children = (System.Collections.IList)values[0];
}
catch( InvalidCastException ice )
{
throw new MappingException(
"Invalid mapping information specified for type " + obj.GetType() + ", check your mapping file for property type mismatches",
ice);
}
}
public object[] GetPropertyValues(object obj) {
NHRepro.Parent t = (NHRepro.Parent)obj;
object[] ret = new object[1];
ret[0] = t.Children;
return ret;
}
}
}
3927 [3340] DEBUG NHibernate.Impl.SessionFactoryObjectFactory (null) - initializing class SessionFactoryObjectFactory
3927 [3340] DEBUG NHibernate.Impl.SessionFactoryObjectFactory (null) - registered: f0a0d0ae876d4aebab708ce12bf71bd1(unnamed)
3927 [3340] INFO NHibernate.Impl.SessionFactoryObjectFactory (null) - no name configured
3927 [3340] DEBUG NHibernate.Impl.SessionFactoryImpl (null) - Instantiated session factory
3927 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - opened session
3927 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - saving [NHRepro.Parent#<null>]
3927 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - executing insertions
3943 [3340] DEBUG NHibernate.Impl.WrapVisitor (null) - Wrapped collection in role: NHRepro.Parent.Children
3943 [3340] DEBUG NHibernate.Persister.EntityPersister (null) - Inserting entity: NHRepro.Parent (native id)
3943 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Opened new IDbCommand, open IDbCommands :1
3943 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Building an IDbCommand object for the SqlString: INSERT INTO [Parent] DEFAULT VALUES; select SCOPE_IDENTITY()
3943 [3340] DEBUG NHibernate.Persister.EntityPersister (null) - Dehydrating entity: [NHRepro.Parent#<null>]
3943 [3340] DEBUG NHibernate.SQL (null) - INSERT INTO [Parent] DEFAULT VALUES; select SCOPE_IDENTITY()
NHibernate: INSERT INTO [Parent] DEFAULT VALUES; select SCOPE_IDENTITY()
3943 [3340] DEBUG NHibernate.Connection.DriverConnectionProvider (null) - Obtaining IDbConnection from Driver
4161 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Opened Reader, open Readers :1
4208 [3340] DEBUG NHibernate.Persister.AbstractEntityPersister (null) - Natively generated identity: 44
4208 [3340] DEBUG NHibernate.Driver.NHybridDataReader (null) - running NHybridDataReader.Dispose()
4208 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Closed Reader, open Readers :0
4208 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Closed IDbCommand, open IDbCommands :0
4208 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - object already associated with session
4208 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - flushing session
4223 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - Flushing entities and processing referenced collections
4223 [3340] DEBUG NHibernate.Impl.AbstractVisitor (null) - Processing collection for role NHRepro.Parent.Children
4223 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - Collection found: [NHRepro.Parent.Children#44], was: [<unreferenced>]
4223 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - Processing unreferenced collections
4223 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - scheduling collection removes/(re)creates/updates
4223 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
4223 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
4223 [3340] DEBUG NHibernate.Impl.Printer (null) - listing entities:
4223 [3340] DEBUG NHibernate.Impl.Printer (null) - NHRepro.Parent{ParentID=44, Children=[Child#-1]}
4223 [3340] DEBUG NHibernate.Impl.SessionImpl (null) - executing flush
4223 [3340] DEBUG NHibernate.Collection.ICollectionPersister (null) - Inserting collection: [NHRepro.Parent.Children#44]
4223 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Opened new IDbCommand, open IDbCommands :1
4223 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Building an IDbCommand object for the SqlString: UPDATE [Child] SET parent_id = :parent_id WHERE child_id = :child_id
4223 [3340] DEBUG NHibernate.Type.Int32Type (null) - binding '44' to parameter: 0
4223 [3340] DEBUG NHibernate.Engine.Cascades (null) - unsaved-value: -1
4239 [3340] DEBUG NHibernate.Impl.BatcherImpl (null) - Closed IDbCommand, open IDbCommands :0
4239 [3340] ERROR NHibernate.Impl.SessionImpl (null) - could not synchronize database state with session
NHibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: NHRepro.Child
at NHibernate.Impl.SessionImpl.ThrowTransientObjectException(Object obj)
at NHibernate.Impl.SessionImpl.GetEntityIdentifierIfNotUnsaved(Object obj)
at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session)
at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand cmd, Object value, Int32 index, ISessionImplementor session)
at NHibernate.Collection.AbstractCollectionPersister.WriteElement(IDbCommand st, Object elt, Boolean writeOrder, ISessionImplementor session)
at NHibernate.Collection.Bag.WriteTo(IDbCommand st, ICollectionPersister persister, Object entry, Int32 i, Boolean writeOrder)
at NHibernate.Collection.AbstractCollectionPersister.Recreate(PersistentCollection collection, Object id, ISessionImplementor session)
at NHibernate.Impl.ScheduledCollectionRecreate.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()