-->
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.  [ 5 posts ] 
Author Message
 Post subject: Trouble with a bidirectional one-to-many-mapping-FK null
PostPosted: Thu Jan 17, 2008 2:08 pm 
Newbie

Joined: Thu Jan 17, 2008 1:33 pm
Posts: 6
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:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 4:36 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
what is inside your addPanel() method?

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 5:04 pm 
Newbie

Joined: Thu Jan 17, 2008 1:33 pm
Posts: 6
public virtual void AddPanel(Panel panel)
{
if (m_Panels.Contains(panel))
{
return; // Don't allow the same panel to be added more than once
}

m_Panels.Add(panel);

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 17, 2008 5:14 pm 
Newbie

Joined: Wed Nov 22, 2006 7:28 am
Posts: 12
MarkSwiatek wrote:
public virtual void AddPanel(Panel panel)
{
if (m_Panels.Contains(panel))
{
return; // Don't allow the same panel to be added more than once
}

m_Panels.Add(panel);

}


Shouldn't you do this because you've defined a bidirectional relationship ?
Using the Inverse=true means Panel.Site is used to define what site this entity relates to (and not m_Panels.Contains(panel) == true)

public virtual void AddPanel(Panel panel)
{
if (m_Panels.Contains(panel))
{
return; // Don't allow the same panel to be added more than once
}

m_Panels.Add(panel);
panel.Site = this;


}


Top
 Profile  
 
 Post subject: That was it.
PostPosted: Thu Jan 17, 2008 7:01 pm 
Newbie

Joined: Thu Jan 17, 2008 1:33 pm
Posts: 6
Thank you very much for the insight. A couple of us were staring at that for a day.


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

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.