-->
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: Delete by Query where Id data type is unknown
PostPosted: Thu Jul 28, 2005 5:01 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
I've got a difficult little problem here - I'm hoping someone can give me a hint.

I'm trying to create a helper method that allows a caller to pass an entity type and id to have an unloaded object deleted:

Code:
public void Delete(Type entityType, object id)


To actually perform the deletion I'm using code like this:

Code:
session.Delete(   String.Format("from {0} entity where entity.id = ?", entityType.Name), id, NHibernateUtil.Object);


The problem is that this doens't work. I get the following exception:

Quote:
Exception: System.IndexOutOfRangeException
Message: Index was outside the bounds of the array.
Source: NHibernate
at NHibernate.Hql.QueryTranslator.PrepareQueryCommand(SqlString sqlString, QueryParameters parameters, Boolean scroll, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Hql.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)

NHibernate.ADOException : Could not execute query
----> System.IndexOutOfRangeException : Index was outside the bounds of the array.
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
at NHibernate.Impl.SessionImpl.Delete(String query, Object[] values, IType[] types)
at NHibernate.Impl.SessionImpl.Delete(String query, Object value, IType type)
at Papa.Framework.Core.Data.Repository.Delete(Type entityType, Object id) in repository.cs:line 311
at Papa.Framework.Core.Services.CustomerServices.DeleteCustomer(Int32 id, ISession session) in customerservices.cs:line 83
at Papa.Framework.Core.Services.CustomerServices.DeleteCustomer(Int32 id) in customerservices.cs:line 75
at Papa.Framework.Core.Tests.Services.CustomerTests.CustomerHighPrivelegeTests.CreateCustomer() in customerhighprivelegetests.cs:line 37
--ADOException
at NHibernate.Hql.QueryTranslator.PrepareQueryCommand(SqlString sqlString, QueryParameters parameters, Boolean scroll, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Hql.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)


If I change the query to specify an NHibernate type that matches the type of the Id property then the query will execute as expected. For instance, if the entity's Id property was an Int32 then the following would work:

Code:
session.Delete(String.Format("from {0} entity where entity.id = ?", entityType.Name), id, NHibernateUtil.Int32);


So, is there any way to do this without having to explicitly specify the NHibernate type? Or is there a way to obtain the NHibernate type of the Id property of the entity at runtime?

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 5:17 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
DOH!

I just found two things:

1. It seems that the version of the code which appears to run correctly actually doesn't delete the record from the database anyway - it just doesn't throw an exception.

and

2. I found this earlier post on a similar question: http://nhibernate.sourceforge.net/forum ... c.php?t=72

Unfortunately Sergey's suggestion in that thread is the approach I was taking and I'm not having much luck.

Still hoping - any suggestions?

Cheers,

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 3:48 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Well, it turns out that the deletion was happening, I was just rolling back a transaction, so I didn't actually see it in the db. *sigh*

However, my original question still stands - is there a way to do this?

Symon.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 6:42 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
Try
Code:
session.Delete(string.Format("from {0} entity where entity.id = {1}", entityType.Name, id));

:wink:

As it is less safe, you should handle the special case: if(id is not a struct) (like string and any user defined class)
And you can do it for each base type also, if you want...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 05, 2005 8:45 am 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Ah hah!

Thanks KPixel - I'm so used to using parameterized queries that I completely overlooked that...

Cheers,

Symon.


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.