-->
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.  [ 9 posts ] 
Author Message
 Post subject: Could not save object
PostPosted: Tue May 24, 2005 4:14 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I get an error 'Could not save object' when I want to save a new object to the database.
The code which does that is as follows:
Code:
public void SaveObject(object obj)
{
   ITransaction trn = this._activeSession.BeginTransaction();
   try
   {
            
      this._activeSession.Save(obj);
      trn.Commit();
   }
   catch (Exception ex)
   {
      trn.Rollback();
      throw ex;
   }
}


_activesession is a ISession which is set by _factory.OpenSession.
_factory is set by SessionFactory.GetInstance().GetNHibernateFactory();

Now I want to check if my application makes a connection to the database anyway. In which object of NHibernate can I check this? I use MSSQL and I set the NHibernate settings in web.config accordingly.
Or does anyone recognizes this error and knows how to solve it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 4:23 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You should look at the inner exception, it contains details on why the object was not saved.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:22 pm 
The inner exception is "Specified cast is not valid.", but this doesn't tell me much.

The contents of the call stack is:

Code:
-   ex   {"Could not save object" }   System.Exception
+   [NHibernate.ADOException]   {NHibernate.ADOException}   NHibernate.ADOException
   System.Object   {NHibernate.ADOException}   System.Object
   _className   null   string
   _COMPlusExceptionCode   -532459699   int
   _exceptionMethod   <undefined value>   System.Reflection.MethodBase
   _exceptionMethodString   null   string
   _helpURL   null   string
   _HResult   -2146232832   int
-   _innerException   {"Specified cast is not valid." }   System.Exception
+   [System.InvalidCastException]   {"Specified cast is not valid." }   System.InvalidCastException
   System.Object   {System.InvalidCastException}   System.Object
   _className   null   string
   _COMPlusExceptionCode   -532459699   int
   _exceptionMethod   <undefined value>   System.Reflection.MethodBase
   _exceptionMethodString   null   string
   _helpURL   null   string
   _HResult   -2147467262   int
   _innerException   { }   System.Exception
   _message   "Specified cast is not valid."   string
   _remoteStackIndex   0   int
   _remoteStackTraceString   null   string
   _source   null   string
+   _stackTrace   {System.Array}   System.Object
   _stackTraceString   null   string
   _xcode   -532459699   int
   _xptrs   0   int
   HelpLink   null   string
   HResult   -2147467262   int
   InnerException   { }   System.Exception
   Message   "Specified cast is not valid."   string
   Source   "NHibernate"   string
   StackTrace   "   at NHibernate.Type.SetType.Wrap(ISessionImplementor session, Object collection)\r\n   at NHibernate.Impl.WrapVisitor.ProcessArrayOrNewCollection(Object collection, PersistentCollectionType collectionType)\r\n   at NHibernate.Impl.WrapVisitor.ProcessCollection(Object collection, PersistentCollectionType collectionType)\r\n   at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)\r\n   at NHibernate.Impl.WrapVisitor.ProcessValues(Object[] values, IType[] types)\r\n   at NHibernate.Impl.SessionImpl.DoSave(Object obj, Key key, IClassPersister persister, Boolean replicate, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)\r\n   at NHibernate.Impl.SessionImpl.DoSave(Object obj, Object id, IClassPersister persister, Boolean useIdentityColumn, CascadingAction cascadeAction, Object anything)\r\n   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)"   string
+   TargetSite   {System.Reflection.RuntimeMethodInfo}   System.Reflection.MethodBase
   _message   "Could not save object"   string
   _remoteStackIndex   0   int
   _remoteStackTraceString   null   string
   _source   null   string
+   _stackTrace   {System.Array}   System.Object
   _stackTraceString   null   string
   _xcode   -532459699   int
   _xptrs   0   int
   HelpLink   null   string
   HResult   -2146232832   int
+   InnerException   {"Specified cast is not valid." }   System.Exception
   Message   "Could not save object"   string
   Source   "NHibernate"   string
   StackTrace   "   at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)\r\n   at NHibernate.Impl.SessionImpl.Save(Object obj)\r\n   at LeagueManager.Core.Service.CoreRepository.SaveObject(Object obj) in e:\\dotnet\\league manager\\core\\leaguemanager.core\\service\\corerepository.cs:line 151"   string
+   TargetSite   {System.Reflection.RuntimeMethodInfo}   System.Reflection.MethodBase


Is the id of the object well defined in my hbm.xml?
Code:
<id name="Id" type="Int32" column="customerid" unsaved-value="-1">
   <generator class="identity"/>
</id>


Top
  
 
 Post subject:
PostPosted: Wed May 25, 2005 3:25 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
Additionally: I added NHibernate to my solution, but I still can't debug NHibernate code. When I want to step in Session.Save(obj), the debugger steps over this line and doesn't go into Session.Save.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 1:37 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
we'll need to see your mapping file and your POCO code. I suspect you have a mismatch between the database column and the field mapping/member. i'm also thinking there might be an error in the id column.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 2:52 pm 
My Customer.hbm.xml looks like this:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="MyApp.Core.Domain.Customer, MyApp.Core" table="customer">
      <id name="Id" type="Int32" column="customerid" unsaved-value="-1">
         <generator class="identity"/>
      </id>
      <property name="Description" column="description" type="String" length="100"/>
      <set name="Orders" table="order">
         <key column="customerid"/>
         <one-to-many class="MyApp.Core.Domain.Order, MyApp.Core"/>
      </set>
   </class>
</hibernate-mapping>


My customer class looks like this:
Code:
using System;
using System.Collections;

namespace MyApp.Core.Domain
{
   /// <summary>
   /// Summary description for Customer.
   /// </summary>
   public class Customer
   {

      private int _id;
      private string _description;
      private IDictionary _orders;

      #region properties

      public virtual int Id
      {
         get { return this._id; }
         set { this._id = value; }
      }

      public virtual string Description
      {
         get { return this._description; }
         set { this._description = value; }
      }

      public virtual IDictionary Orders
      {
         get
         {
            if (_orders == null) _orders = new Hashtable();
            return _orders;
         }
         set { _orders = value; }
      }

      #endregion

      public Customer()
      {
         this._id = -1;
      }

   }
}



Top
  
 
 Post subject:
PostPosted: Thu May 26, 2005 6:12 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
i'm not seeing anything here. pretty straight-forward. can you enable log4net and post some of the DEBUG output?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 8:40 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 12:59 pm
Posts: 20
IDictionary and <set> are not the same thing.

http://wiki.nhibernate.org/display/NH/Frequently+Asked+Questions

There's a section on collections.

I thought we had the Property Setters throwing a better exception than that, but apparently not...


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 3:11 pm 
Beginner
Beginner

Joined: Tue May 17, 2005 2:48 pm
Posts: 47
I solved the problem. I replaced IDictionary by IList and I replaced <set> by <bag>. Now it works fine.
Thanx for your help.


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