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.  [ 4 posts ] 
Author Message
 Post subject: IQuery.SetString - Order By Clause
PostPosted: Mon Jun 05, 2006 12:08 pm 
Newbie

Joined: Tue May 30, 2006 5:50 pm
Posts: 6
Hello all. I recently started using named HQL queries and I think I've run into my first problem. I have an HQL query that is in my mapping file, I want the Sort Field and Sort Direction to be parametrized (see below). When i execute my query without the parametrized Order By clause it works perfect, with the Order By clause I received an "Index Out Of Range" exception (Target = PopulateSqlString).

FROM
Ticket as ticket
LEFT JOIN FETCH ticket.IntranetUser
LEFT JOIN FETCH ticket.Department
LEFT JOIN FETCH ticket.Branch
WHERE
ticket.IntranetUserID = :userID
AND
ticket.TicketStatus = :ticketStatus
ORDER BY :sortField :sortDirection

I could not find any documentation stating that named parameters were not valid in the context of the Order By clause. Has anyone else attempted to do this? Any help would be greatly appreciated, I'm currently digging through the NHibernate source to see if i can't determine if this is intentional or a bug.

Here is the code that is executing the query (with named parameters for the order by clause).

IList ut;

ISession s = _sessionFactory.OpenSession();

ITransaction t = s.BeginTransaction();

try
{
IQuery q = s.GetNamedQuery("GetUserTickets");

q.SetInt32("userID", intranetUserID);
q.SetEnum("ticketStatus", status);
q.SetString("sortField", sortField);
q.SetString("sortDirection", GetSortDirectionString(sortDirection));

ut = q.List();

t.Commit();
}
catch (Exception ex)
{
t.Rollback();

throw new ApplicationException("An exception occured while trying to get user tickets.", ex);
}
finally
{
s.Close();
}

return ut;

Thanks in advance.

_________________
-Beau


Top
 Profile  
 
 Post subject: I found the answer :o(
PostPosted: Mon Jun 05, 2006 12:28 pm 
Newbie

Joined: Tue May 30, 2006 5:50 pm
Posts: 6
Ok, after downloading the source and debugging i've come to the conclusion the named parameters are 1-1 with command parameters (SqlClient in my case). So unfortunately adding parameters can only be done when assigning values to columns.

Here is the code.

for( int i = 0; i < paramTypeList.Count; i++ )
{
IType type = ( IType ) paramTypeList[ i ];
string[ ] colNames = new string[type.GetColumnSpan( factory )];

for( int j = 0; j < colNames.Length; j++ )
{
colNames[ j ] = "p" + paramIndex.ToString() + j.ToString();
}

Parameter[ ] sqlParameters = Parameter.GenerateParameters( factory, colNames, type );

foreach( Parameter param in sqlParameters )
{
sqlPartIndex = paramIndexes[ paramIndex ];
sql.SqlParts[ sqlPartIndex ] = param;

paramIndex++;
}
}

Bah. I'll try a different way.

_________________
-Beau


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 05, 2006 1:05 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
the only way i've been able to do this is with inline IQuery objects:

Code:
public IList GetAssignedTasks(Member assignee, string orderByField)
{
   StringBuilder query = new StringBuilder();

   query.Append("from MemberTask as mt where mt.Member = :member and mt.DateCompleted is null order by ");

   if (orderByField != null)
   {
      query.Append(String.Format("mt.Task.{0} asc", orderByField));
   }
   else
   {
      // default
      query.Append("mt.Task.DateDue asc");
   }

   return this.dbSession.CreateQuery(query.ToString())
      .SetEntity("member", assignee)
                .List();

}


although I've wondered if you could do something like:

Code:
session.GetNamedQuery("namedQuery")
    .QueryString.Concat("order by :param")
    .SetParamater("param", param)
    .List();


seems like a hack, but i was looking for a way to get the benefit of keeping the main part of the query in the mapping file...


Top
 Profile  
 
 Post subject: Thanks a lot
PostPosted: Tue Aug 29, 2006 1:13 am 
Newbie

Joined: Tue Aug 29, 2006 12:41 am
Posts: 1
Hi, thanks for the solution, I'm with this issue for about a two hours, but, the code that you list don“t works for me (I had to use a inline query) :(.

Do you post this issue in the bug tracking of NHibernate (JIRA)?, is this solved?

Regards.

_________________
Alvaro S.
Cochabamba - Bolivia


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