Returning extra data from HQL
I'm writing an HQL query, in the form of:
from a where ... or exists (from b inner join b.c where ...) and ...
I get back an entity of type a
from this query. However, I need to also know whether the exists
clause came back true or not later on in my code. If this was SQL, I'd tack it onto the select list. However, even if I add to the HQL select clause, so that it becomes:
select a, exists (from b inner join b.c where ...) as x from a where ... or x and ...
In my code I now have to choose between viewing untyped data, or viewing typed entities of type a
and throwing away my value x
that came back with i开发者_如何学Ct.
Is there a way to somehow get back typed data plus the extra column?
You have two options here as I see it:
1) Add the additional property as a read-only mapped property to your entity, making use of the formula
mapping to derive the value using inline sql or a udf.
2) Get only the data you need back using a custom hql query and use the aliastobeanresulttransformer
to project to a strongly-typed dto that includes your fields of interest.
Which route you go is really up to you, but I'd tend to lean towards option #2 so as not to pollute your domain model with unneeded fields.
精彩评论