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: Using parameter name with number ending may lead to problems
PostPosted: Fri Mar 07, 2008 3:49 am 
Newbie

Joined: Fri Mar 07, 2008 1:53 am
Posts: 2
Hibernate version:
NHibernate 1.2.1.GA

Mapping documents:
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/quickstart.html#quickstart-mapping

Code between sessionFactory.openSession() and session.close():
First one:
Code:
query = session.CreateQuery("select c from Cat as c where c.Id in (:qw11) or c.Name in (:qw1)");
query.SetParameterList("qw11", new string[] {"I", "J"});
query.SetParameterList("qw1", new string[] {"Princess", "Prince", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"});
foreach (Cat cat in query.Enumerable())
{
    Console.Out.WriteLine("Cat: " + cat.Name);
}

Second one:
Code:
query = session.CreateQuery("select c from Cat as c where c.Id in (:qw1) or c.Name in (:qw10)");
query.SetParameterList("qw1", new string[] {"Princess"});
query.SetParameterList("qw10", new string[] {"Princess"});
foreach (Cat cat in query.Enumerable())
{
    Console.Out.WriteLine("Cat: " + cat.Name);
}


Full stack trace of any exception that occurs:
First one:
Quote:
Unhandled Exception: System.ArgumentException: Item has already been added. Key in dictionary: 'qw110_' Key being added: 'qw110_'
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at NHibernate.Impl.AbstractQueryImpl.BindParameterList(String queryString, String name, TypedValue typedList, IDictionary namedParams)
at NHibernate.Impl.AbstractQueryImpl.BindParameterLists(IDictionary namedParams)
at NHibernate.Impl.QueryImpl.Enumerable()
...

Second one:
Quote:
Unhandled Exception: NHibernate.QueryException: Named parameter does not appear in Query: qw100_ [select c from QuickStart.Cat as c where c.Id in (:qw10_) or c.Name in (:qw10_00_)]
at NHibernate.Hql.Classic.QueryTranslator.GetNamedParameterLocs(String name)
at NHibernate.Loader.Loader.GetParameterTypes(QueryParameters parameters, Boolean addLimit, Boolean addOffset)
at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters parameters, Boolean scroll, ISessionImplementor session)
at NHibernate.Hql.Classic.QueryTranslator.GetEnumerable(QueryParameters parameters, ISessionImplementor session)
at NHibernate.Impl.SessionImpl.Enumerable(String query, QueryParameters parameters)
at NHibernate.Impl.QueryImpl.Enumerable()
...


Name and version of the database you are using:
PostgreSQL 8.2.6


I just append the names with "_" to prevent the problems.

Thanks,
Shing


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 09, 2008 11:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Thanks for the tip.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 09, 2008 11:44 pm 
Newbie

Joined: Fri Mar 07, 2008 1:53 am
Posts: 2
Any fix for the problems?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 12:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
A JIRA issue has been created (NH-1253). Patches are welcomed.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 1:04 pm 
Regular
Regular

Joined: Wed Jan 25, 2006 1:11 am
Posts: 118
Location: Copenhagen, Denmark
I have given it a shot at a patch. As you figured out yourself its because of name clashes in parameters. But appending with _ could also give you some trouble so to avoid this problems i would suggest that any named parameters should not be a substring of another that is:

:foo and :foobar in the same query isnt very good currently as far as i can see, but i can be wrong


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.