-->
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: Cannot save simple parent-child, getting cast exception
PostPosted: Sun Mar 05, 2006 1:16 am 
Newbie

Joined: Sat Feb 25, 2006 3:30 pm
Posts: 13
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

I am using NHibernate, the HowToAskForHelp says to read the Quick Start -- which I have.

I have setup a very simple parent to child model. When I try to save the
first Parent, I get an Invalid Cast Exception. When I remove the <set> tag from the Parent schema, I don't get the error.[/quote]

Can anyone give me a suggestion as to the problem, or a technique to go
about troubleshooting this?

Many thanks,
Steve Brennan

Hibernate version: NHibernate 1.0.2

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHRepro.Parent, NHRepro" table="[Parent]">
<id name="ParentID" column="parent_id" type="int" unsaved-value="-1">
<generator class="native" />
</id>
<set name="Children">
<key column="parent_id" />
<one-to-many class="NHRepro.Child, NHRepro" />
</set>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHRepro.Child, NHRepro" table="[Child]">
<id name="ChildID" column="child_id" type="int" unsaved-value="-1">
<generator class="native" />
</id>
<many-to-one name="Parent" column="parent_id" not-null="true" />
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Parent p = new Parent();
session.Save(p); // BANG!!
session.Flush();


Full stack trace of any exception that occurs:
Could not save object
at NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj,
CascadingAction action, Object anything)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at NHRepro.EntryPoint.Main() in c:\brennan\nhrepro\entrypoint.cs:line 26

Specified cast is not valid.
System.InvalidCastException: Specified cast is not valid.
at NHibernate.Type.SetType.Wrap(ISessionImplementor session, Object
collection)
at NHibernate.Impl.WrapVisitor.ProcessArrayOrNewCollection(Object
collection, PersistentCollectionType collectionType)
at NHibernate.Impl.WrapVisitor.ProcessCollection(Object collection,
PersistentCollectionType collectionType)
at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)
at NHibernate.Impl.WrapVisitor.ProcessValues(Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, 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)The program '[3444] NHRepro.exe' has
exited with code 0 (0x0).


Name and version of the database you are using:
MS SQL Server 2000 SP3

The generated SQL (show_sql=true):
How/where to do I set this in NHibernate?
I have added the following to the nhibernate section of my config file:
<add key="hibernate.show_sql" value="true" />
I have not seen any new output and I don't know where to look.


Debug level Hibernate log excerpt:
281 [3764] INFO NHibernate.Cfg.Environment (null) - NHibernate 1.0.2
734 [3764] INFO NHibernate.Cfg.Environment (null) - Using reflection optimizer
734 [3764] INFO NHibernate.Cfg.Configuration (null) - Searching for mapped documents in assembly: NHRepro
750 [3764] INFO NHibernate.Cfg.Configuration (null) - Found mapping documents in assembly: NHRepro.Child.hbm.xml
812 [3764] INFO NHibernate.Dialect.Dialect (null) - Using dialect: NHibernate.Dialect.MsSql2000Dialect
843 [3764] INFO NHibernate.Cfg.Binder (null) - Mapping class: NHRepro.Child -> [Child]
859 [3764] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: ChildID -> child_id, type: Int32
890 [3764] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: Parent -> parent_id, type: Parent
890 [3764] INFO NHibernate.Cfg.Configuration (null) - Found mapping documents in assembly: NHRepro.Parent.hbm.xml
890 [3764] INFO NHibernate.Dialect.Dialect (null) - Using dialect: NHibernate.Dialect.MsSql2000Dialect
890 [3764] INFO NHibernate.Cfg.Binder (null) - Mapping class: NHRepro.Parent -> [Parent]
890 [3764] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: ParentID -> parent_id, type: Int32
906 [3764] DEBUG NHibernate.Cfg.Binder (null) - Mapped property: Children, type: ISet
906 [3764] INFO NHibernate.Cfg.Configuration (null) - processing one-to-many association mappings
906 [3764] DEBUG NHibernate.Cfg.Binder (null) - Second pass for collection: NHRepro.Parent.Children
906 [3764] INFO NHibernate.Cfg.Binder (null) - mapping collection: NHRepro.Parent.Children -> [Child]
921 [3764] DEBUG NHibernate.Cfg.Binder (null) - Mapped collection key: parent_id, one-to-many: Child
921 [3764] INFO NHibernate.Cfg.Configuration (null) - processing one-to-one association property references
921 [3764] INFO NHibernate.Cfg.Configuration (null) - processing foreign key constraints
921 [3764] DEBUG NHibernate.Cfg.Configuration (null) - resolving reference to class: Parent
921 [3764] INFO NHibernate.Dialect.Dialect (null) - Using dialect: NHibernate.Dialect.MsSql2000Dialect
921 [3764] INFO NHibernate.Cfg.SettingsFactory (null) - use outer join fetching: True
921 [3764] INFO NHibernate.Connection.ConnectionProviderFactory (null) - Intitializing connection provider: NHibernate.Connection.DriverConnectionProvider
921 [3764] INFO NHibernate.Connection.ConnectionProvider (null) - Configuring ConnectionProvider
937 [3764] INFO NHibernate.Cfg.SettingsFactory (null) - Optimize cache for minimal puts: False
937 [3764] INFO NHibernate.Cfg.SettingsFactory (null) - Query language substitutions: {}
937 [3764] INFO NHibernate.Cfg.SettingsFactory (null) - cache provider: NHibernate.Cache.HashtableCacheProvider
937 [3764] INFO NHibernate.Cfg.Configuration (null) - instantiating and configuring caches
937 [3764] INFO NHibernate.Impl.SessionFactoryImpl (null) - building session factory
937 [3764] 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}
984 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Init compiler for class NHRepro.Child
984 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\nhibernate.dll
984 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly C:\brennan\NHRepro\bin\Debug\NHRepro.exe
984 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll
984 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\log4net.dll
984 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll
1203 [3764] 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;
}
}
}

1218 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Init compiler for class NHRepro.Parent
1218 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\nhibernate.dll
1218 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly C:\brennan\NHRepro\bin\Debug\NHRepro.exe
1218 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll
1218 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\brennan\nhrepro\bin\debug\log4net.dll
1218 [3764] DEBUG NHibernate.Persister.GetSetHelperFactory (null) - Adding referenced assembly c:\windows\assembly\gac\system\1.0.5000.0__b77a5c561934e089\system.dll
1421 [3764] 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;
}
}
}

1515 [3764] DEBUG NHibernate.Impl.SessionFactoryObjectFactory (null) - initializing class SessionFactoryObjectFactory
1515 [3764] DEBUG NHibernate.Impl.SessionFactoryObjectFactory (null) - registered: 4adfc94f11ce472b819530cea231566a(unnamed)
1515 [3764] INFO NHibernate.Impl.SessionFactoryObjectFactory (null) - no name configured
1515 [3764] DEBUG NHibernate.Impl.SessionFactoryImpl (null) - Instantiated session factory
1531 [3764] DEBUG NHibernate.Impl.SessionImpl (null) - opened session
1531 [3764] DEBUG NHibernate.Impl.SessionImpl (null) - saving [NHRepro.Parent#<null>]
1531 [3764] DEBUG NHibernate.Impl.SessionImpl (null) - executing insertions
1546 [3764] DEBUG NHibernate.Util.ADOExceptionReporter (null) - Could not save object
System.InvalidCastException: Specified cast is not valid.
at NHibernate.Type.SetType.Wrap(ISessionImplementor session, Object collection)
at NHibernate.Impl.WrapVisitor.ProcessArrayOrNewCollection(Object collection, PersistentCollectionType collectionType)
at NHibernate.Impl.WrapVisitor.ProcessCollection(Object collection, PersistentCollectionType collectionType)
at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)
at NHibernate.Impl.WrapVisitor.ProcessValues(Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, 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)
1562 [3764] WARN NHibernate.Util.ADOExceptionReporter (null) - System.InvalidCastException: Specified cast is not valid.
at NHibernate.Type.SetType.Wrap(ISessionImplementor session, Object collection)
at NHibernate.Impl.WrapVisitor.ProcessArrayOrNewCollection(Object collection, PersistentCollectionType collectionType)
at NHibernate.Impl.WrapVisitor.ProcessCollection(Object collection, PersistentCollectionType collectionType)
at NHibernate.Impl.AbstractVisitor.ProcessValue(Object value, IType type)
at NHibernate.Impl.WrapVisitor.ProcessValues(Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.DoSave(Object theObj, 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)
1562 [3764] ERROR NHibernate.Util.ADOExceptionReporter (null) - Specified cast is not valid.


Top
 Profile  
 
 Post subject: Adding SQL Trace
PostPosted: Sun Mar 05, 2006 11:23 am 
Newbie

Joined: Sat Feb 25, 2006 3:30 pm
Posts: 13
I used Profiler to catch the SQL generated by my repro app.

exec sp_reset_connection
exec sp_reset_connection
INSERT INTO [Parent] DEFAULT VALUES; select SCOPE_IDENTITY()

INSERT INTO [Parent] DEFAULT VALUES;


INSERT INTO [Parent] DEFAULT VALUES;
select SCOPE_IDENTITY()
select SCOPE_IDENTITY()
INSERT INTO [Parent] DEFAULT VALUES; select SCOPE_IDENTITY()


Top
 Profile  
 
 Post subject: Case Closed
PostPosted: Mon Mar 06, 2006 11:06 am 
Newbie

Joined: Sat Feb 25, 2006 3:30 pm
Posts: 13
I don't know how to close a thread, but I am no longer getting the exception, I was missing a class attribute on the many-to-one element.

Thanks,
Steve Brennan


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.