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