-->
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.  [ 3 posts ] 
Author Message
 Post subject: Null value for many-to-one object on save.
PostPosted: Fri Jul 29, 2005 2:14 pm 
I'm trying to save an object that has some many-to-one relations defined. When I save, it creates an insert statement and picks up all of the many-to-one relations just fine except for one. They are all declared using the same syntax. All of the values have objects loaded in, and I stepped through my project with the debugger, and when save is called, NH goes in and picks up the object from my class, but somewhere between there, and the insert, it makes the key value NULL.

Hopefully I explained that clearly enough. What am I doing wrong?

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="HR.Models.Review, HR.Core" table="reviews">

      <id name="ReviewID" column="review_id" type="Int32" unsaved-value="0">
         <generator class="native"/>
      </id>
      <many-to-one name="Region" column="region_id" class="HR.Models.Region, HR.Core" />
      <many-to-one name="EcType" column="ec_type_id" class="HR.Models.EcType, HR.Core" />
      <property name="EmployeeID" column="employee_id" type="String" not-null="true" length="6" />
      <property name="StartDate" column="start_date" type="DateTime" not-null="true" />
      <property name="EndDate" column="end_date" type="DateTime" not-null="true" />
      <property name="ManagerEmployeeID" column="manager_employee_id" type="String" not-null="true" length="6" />
      <property name="LastUpdated" column="last_updated" type="DateTime" not-null="true" />
      <many-to-one name="Status" column="status_id" class="HR.Models.ReviewStatus, HR.Core" />
      <property name="MonthsInReview" column="months_in_review" type="Int32" not-null="true" />
      
      <one-to-one name="GoalAcknowledgement" class="HR.Models.GoalAcknowledgement, HR.Core" constrained="false"/>
      
      <bag name="ResultAcknowledgements" inverse="true" lazy="true" order-by="month_number asc">
         <key column="review_id"/>
         <one-to-many class="HR.Models.ResultAcknowledgement, HR.Core"/>
      </bag>
   </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="HR.Models.ReviewStatus, HR.Core" table="review_statuses" mutable="false">

      <id name="StatusID" column="status_id" type="Int32" unsaved-value="0">
         <generator class="assigned"/>
      </id>
      <property name="Status" column="status" type="String" not-null="true" length="100" />
      
   </class>
</hibernate-mapping>



DEBUG 20050729-10:58:09 2432 NHibernate.Impl.SessionImpl SaveOrUpdate() unsaved instance
INFO 20050729-10:58:09 2432 NHibernate.Impl.SessionImpl executing insertions
DEBUG 20050729-10:58:48 2432 NHibernate.Engine.Cascades unsaved-value: 0
DEBUG 20050729-10:58:48 2432 NHibernate.Engine.Cascades unsaved-value: 0
DEBUG 20050729-10:58:48 2432 NHibernate.Engine.Cascades unsaved-value: 0
DEBUG 20050729-10:58:48 2432 NHibernate.Persister.EntityPersister Inserting entity: Apollo.HR.TotalSuccessReview.Models.Review (native id)
DEBUG 20050729-10:58:48 2432 NHibernate.Impl.BatcherImpl about to open: 0 open IDbCommands, 0 open DataReaders
DEBUG 20050729-10:58:48 2432 NHibernate.Impl.BatcherImpl Building an IDbCommand object for the SqlString: INSERT INTO reviews (region_id, manager_employee_id, start_date, end_date, status_id, employee_id, last_updated, ec_type_id, months_in_review) VALUES (:region_id, :manager_employee_id, :start_date, :end_date, :status_id, :employee_id, :last_updated, :ec_type_id, :months_in_review); select SCOPE_IDENTITY()
DEBUG 20050729-10:58:48 2432 NHibernate.Persister.EntityPersister Dehydrating entity: Apollo.HR.TotalSuccessReview.Models.Review#
DEBUG 20050729-10:58:48 2432 NHibernate.Engine.Cascades unsaved-value: 0
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '7' to parameter: 0
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '' to parameter: 1
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '6/1/2004' to parameter: 2
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '12/1/2004' to parameter: 3
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding null to parameter: 4
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '' to parameter: 5
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '7/29/2005' to parameter: 6
DEBUG 20050729-10:58:48 2432 NHibernate.Engine.Cascades unsaved-value: 0
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '1' to parameter: 7
DEBUG 20050729-10:58:48 2432 NHibernate.Type.NullableType binding '6' to parameter: 8
INFO 20050729-10:58:48 2432 NHibernate.Impl.BatcherImpl Preparing INSERT INTO reviews (region_id, manager_employee_id, start_date, end_date, status_id, employee_id, last_updated, ec_type_id, months_in_review) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8); select SCOPE_IDENTITY()
DEBUG 20050729-10:58:48 2432 NHibernate.Connection.DriverConnectionProvider Obtaining IDbConnection from Driver
ERROR 20050729-10:58:48 2432 NHibernate.Persister.EntityPersister Cannot insert the value NULL into column 'status_id', table 'HR5_0.dbo.reviews'; column does not allow nulls. INSERT fails.
Exception: System.Data.SqlClient.SqlException
Message: Cannot insert the value NULL into column 'status_id', table 'HR5_0.dbo.reviews'; column does not allow nulls. INSERT fails.
Source: .Net SqlClient Data Provider
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)

DEBUG 20050729-10:58:48 2432 NHibernate.Impl.BatcherImpl done closing: 0 open IDbCommands, 0 open DataReaders
ERROR 20050729-10:58:48 2432 NHibernate.ADOException Could not save object
Exception: System.Data.SqlClient.SqlException
Message: Cannot insert the value NULL into column 'status_id', table 'HR5_0.dbo.reviews'; column does not allow nulls. INSERT fails.
Source: .Net SqlClient Data Provider
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader()
at NHibernate.Impl.BatcherImpl.ExecuteReader(IDbCommand cmd)
at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledIdentityInsertion.Execute()
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)


Top
  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 5:29 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Could it be that the object's Status.StatusID is zero, and so NHibernate recognizes it as an unsaved object?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 30, 2005 1:06 pm 
Newbie

Joined: Sat Jul 30, 2005 1:02 pm
Posts: 1
Hi, I'm the original poster.

Yes, the StatusID is 0, and 0 is a valid status. Is it just a question of changing the unsaved-value attribute to -1 or some such?

Thanks,
Steve


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.