Site has a one-to-many relationship with Panel. Attempting to save a site with one panel. From the log, the SiteId is properly generated, but the insert to the Panel table binds a null to the Site_Id column.
Hibernate version: 1.2.1GA
Mapping documents:
Site.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Bosch.RPS.DomainModel"
assembly="Bosch.RPS.DomainModel">
<class name="Site" table="Site">
<id name="Id" access="nosetter.pascalcase-m-underscore" column="Id" unsaved-value="0" type="Int32">
<generator class="identity" />
</id>
<property name="Name" type="String" length="50" column="Name" not-null="true"/>
<bag name="Panels" inverse="true" access="nosetter.pascalcase-m-underscore" lazy="true" cascade="save-update">
<key column="Site_Id" />
<one-to-many class="Panel"/>
</bag>
</class>
</hibernate-mapping>
Panel.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="Bosch.RPS.DomainModel"
assembly="Bosch.RPS.DomainModel">
<class name="Panel" table="Panel">
<id name="Id" access="nosetter.pascalcase-m-underscore" column="Id" unsaved-value="0" type="Int32">
<generator class="identity" />
</id>
<property name="Name" type="String" length="50" column="Name" not-null="true"/>
<many-to-one name="Site" class="Site" column="Site_Id" access="nosetter.pascalcase-m-underscore" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
using (ITransaction tx = session.BeginTransaction())
{
Site s = TestHelper.GetTestSite();
Panel p = new Panel("A.Panel");
s.AddPanel(p);
session.SaveOrUpdate(s);
}
Full stack trace of any exception that occurs:
System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Site_Id', table 'RPS.dbo.Panel'; column does not allow nulls. INSERT fails.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlCommandInfo sql, Object obj, ISessionImplementor session)
Name and version of the database you are using:
Sql Server Express 2005
The generated SQL (show_sql=true):
13:00:35,051 DEBUG [TestRunnerThread] SessionImpl: PID=4648 opened session
13:00:35,051 DEBUG [TestRunnerThread] AdoTransaction: PID=4648 begin
13:00:35,066 DEBUG [TestRunnerThread] DriverConnectionProvider: PID=4648 Obtaining IDbConnection from Driver
13:00:35,394 DEBUG [TestRunnerThread] Cascades: PID=4648 unsaved-value: 0
13:00:35,410 DEBUG [TestRunnerThread] SessionImpl: PID=4648 SaveOrUpdate() unsaved instance
13:00:35,425 DEBUG [TestRunnerThread] SessionImpl: PID=4648 saving [Bosch.RPS.DomainModel.Site#<null>]
13:00:35,425 DEBUG [TestRunnerThread] Cascades: PID=4648 processing cascades for: Bosch.RPS.DomainModel.Site
13:00:35,441 DEBUG [TestRunnerThread] Cascades: PID=4648 done processing cascades for: Bosch.RPS.DomainModel.Site
13:00:35,441 DEBUG [TestRunnerThread] SessionImpl: PID=4648 executing insertions
13:00:35,472 DEBUG [TestRunnerThread] WrapVisitor: PID=4648 Wrapped collection in role: Bosch.RPS.DomainModel.Site.Panels
13:00:35,488 DEBUG [TestRunnerThread] AbstractEntityPersister: PID=4648 Inserting entity: Bosch.RPS.DomainModel.Site (native id)
13:00:35,519 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Opened new IDbCommand, open IDbCommands: 1
13:00:35,519 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Building an IDbCommand object for the SqlString: INSERT INTO RPS.dbo.Site (Name) VALUES (?); select SCOPE_IDENTITY()
13:00:35,535 DEBUG [TestRunnerThread] AbstractEntityPersister: PID=4648 Dehydrating entity: [Bosch.RPS.DomainModel.Site#<null>]
13:00:35,535 DEBUG [TestRunnerThread] StringType: PID=4648 binding 'site.name' to parameter: 0
13:00:35,550 DEBUG [TestRunnerThread] SQL: PID=4648 INSERT INTO RPS.dbo.Site (Name) VALUES (@p0); select SCOPE_IDENTITY(); @p0 = 'site.name'
13:00:35,629 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Opened IDataReader, open IDataReaders: 1
13:00:35,644 DEBUG [TestRunnerThread] Int32Type: PID=4648 returning '80' as column:
13:00:35,660 DEBUG [TestRunnerThread] AbstractEntityPersister: PID=4648 Natively generated identity: 80
13:00:35,660 DEBUG [TestRunnerThread] NHybridDataReader: PID=4648 running NHybridDataReader.Dispose()
13:00:35,675 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Closed IDataReader, open IDataReaders :0
13:00:35,675 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Closed IDbCommand, open IDbCommands: 0
13:00:35,691 DEBUG [TestRunnerThread] Cascades: PID=4648 processing cascades for: Bosch.RPS.DomainModel.Site
13:00:35,707 DEBUG [TestRunnerThread] Cascades: PID=4648 cascading to collection: Bosch.RPS.DomainModel.Site.Panels
13:00:35,707 DEBUG [TestRunnerThread] Cascades: PID=4648 cascading to SaveOrUpdate()
13:00:35,722 DEBUG [TestRunnerThread] Cascades: PID=4648 unsaved-value: 0
13:00:35,722 DEBUG [TestRunnerThread] SessionImpl: PID=4648 SaveOrUpdate() unsaved instance
13:00:35,722 DEBUG [TestRunnerThread] SessionImpl: PID=4648 saving [Bosch.RPS.DomainModel.Panel#<null>]
13:00:35,722 DEBUG [TestRunnerThread] SessionImpl: PID=4648 executing insertions
13:00:35,754 DEBUG [TestRunnerThread] AbstractEntityPersister: PID=4648 Inserting entity: Bosch.RPS.DomainModel.Panel (native id)
13:00:35,769 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Opened new IDbCommand, open IDbCommands: 1
13:00:35,769 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Building an IDbCommand object for the SqlString: INSERT INTO RPS.dbo.Panel (Name, Site_Id) VALUES (?, ?); select SCOPE_IDENTITY()
13:00:35,785 DEBUG [TestRunnerThread] AbstractEntityPersister: PID=4648 Dehydrating entity: [Bosch.RPS.DomainModel.Panel#<null>]
13:00:35,785 DEBUG [TestRunnerThread] StringType: PID=4648 binding 'A.Panel' to parameter: 0
13:00:35,800 DEBUG [TestRunnerThread] Int32Type: PID=4648 binding null to parameter: 1 13:00:35,816 DEBUG [TestRunnerThread] SQL: PID=4648 INSERT INTO RPS.dbo.Panel (Name, Site_Id) VALUES (@p0, @p1); select SCOPE_IDENTITY(); @p0 = 'A.Panel', @p1 = ''
13:00:35,816 DEBUG [TestRunnerThread] BatcherImpl: PID=4648 Closed IDbCommand, open IDbCommands: 0
13:00:35,832 DEBUG [TestRunnerThread] ADOExceptionReporter: PID=4648 could not insert: [Bosch.RPS.DomainModel.Panel] [INSERT INTO RPS.dbo.Panel (Name, Site_Id) VALUES (?, ?)]
Debug level Hibernate log excerpt:
|