-->
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.  [ 2 posts ] 
Author Message
 Post subject: Run-time modification of named queries?
PostPosted: Fri Jan 06, 2006 8:23 am 
Beginner
Beginner

Joined: Mon Dec 19, 2005 4:13 am
Posts: 27
Location: Prague, Czech Republic
Hello,

please can some help me with the following problem with named HQL queries?

I am using factory classes with instance methods FindBySomething() to execute named queries. My factory class hiearchy is the same as the hiearchy of bussines entities.

Factory class for abstract Subject entity may looks like this:

Code:
  public class SubjectFactory<T> : BusinessEntityFactory<T> where T : Subject {
    ...
   
    public T[] FindByRating(SubjectRating rating) {
      IList objectOutput = ...CurrentSession.GetNamedQuery("SubjectsByRating")
        .SetEntity("rating", rating)
        .List();

      T[] genericOutput = GetConvertor().ToArray(objectOutput);
      return genericOutput;
    }
  }


Factory class for Company entity which extends Subject entity looks like this:

Code:
  public class CompanyFactory<T> : SubjectFactory<T> where T : Company {
  }


The point is, that I want now use FindByRating method on the Company factory without writing any code. This actually works very fine when using Criteria queries or the queries written directly in the class code, where query is constructed by something like this:

Code:
  string query = String.Format("from {0} subject where subject.Rating = :rating", typeof(T).Name);


The problem is with the named queries, because I am not able to modify the "Subject" with the "Company" to restrict result to instances which factory class is managing. To get this work, I need to write something like this:

Code:
  Query query = CurrentSession.GetNamedQuery("SubjectsByRating");
  query.QueryString = query.QueryString.Replace("_Subject_", typeof(T).Name);
  IList objectOutput = query.SetEntity("rating", rating).List();


Please is there some API how to do this or is there any other way how to use named queries in this manner? I can still get named query and use its QueryString to create new query, but I am afraid that the query will by parsed two times unnecessarily.

Many thanks.
David


Top
 Profile  
 
 Post subject: Modifying query at runtime - please help!
PostPosted: Mon Jan 23, 2006 5:17 am 
Beginner
Beginner

Joined: Mon Dec 19, 2005 4:13 am
Posts: 27
Location: Prague, Czech Republic
I have another problem with this issue. I need to control sorting on prepared HQL or SQL quuery at runtime by adding "order by" to the end of query string. To do so, I just need to modify the querystring of already existing IQuery. Is there any way how to do it (construct new query based on existing query, with already assigned parameters and other attributes, but with some modifacations)?


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