In Grails I would like a transient variable to be set based on a SQL query. I do have to use SQL, not HQL.
(This is a simplified example. Using Grails 3.0.11.)
- Do a SQL query
- As part of the SQL query calculate a value for a column
- Have that value set in the Entity class added to the SQL Query
- See
ofLegalAge
/of_legal_age
below
Lets say I have the following Domain class
class Person {
String firstName
String lastName
Integer age
static mapping = {table "person"}
}
Then I have a view on a Person Domain class as follows
class PersonView {
Long id
String firstName
String lastName
// ofLegalAge is dynamically set based on query
Boolean ofLegalAge
// Other things based on joins
static transients = [ "ofLegalAge" ]
static mapping = {table "person_view"}
}
What I would like to do is
String sql = """select id,
first_name,
last_name,
-- This is the source for of_legal_age
case when age >= :ageForThisArea
then true
else false
end as of_legal_age
from view_person
where age >= :ageForThisArea"""
def session = sessionFactory.getCurrentSession()
def query = session.createSQLQuery(sql)
query.setParameter("ageForThisArea", age)
query.addEntity(PersonView)
// This should result in a List of PersonView with ofLegalAge set correctly
List<PersonView> theList = query.list()
Basically at the end I need ofLegalAge
to be set based on the query, however at the end (after query.list()
is called) it is not being set in PersonView
.