I have around 40 fields that are requested as individual set of data based on a date range. I am trying to pass the column name for the field as a parameter to the named query and it does not seem to be working. How can I do that? Is it even possible?
@NamedNativeQuery(name = "fbs.fieldValue", query = "select ft1.rep_date as date, :fieldName as value from asset a, fundamental_t1 ft1, fundamental_bs fbs " +
"where a.asset_key = :assetKey and ft1.asset_id = a.id and fbs.fundamental_id = ft1.id and rep_date is not null and " +
"rep_date >= :startDate and rep_date <= :endDate order by rep_date", resultSetMapping = "fbs.fieldValue")
Code between sessionFactory.openSession() and session.close():
Query q = getSession().getNamedQuery(getFielValueQueryName());
q.setString("assetKey", assetKey);
q.setString("fieldName", fieldName);
q.setDate("startDate", startDate);
q.setDate("endDate", endDate);
Full stack trace of any exception that occurs:
Name and version of the database you are using: Mysql
The generated SQL (show_sql=true):
select ft1.rep_date as date, ? as value from asset a, fundamental_t1 ft1, fundamental_bs fbs where a.asset_key = ? and ft1.asset_id = a.id and fbs.fundamental_id = ft1.id and rep_date is not null and rep_date >= ? and rep_date <= ? order by rep_date
No error occurs but the result returned are in the format:
<date>, assets_total
Basically the field name shows up as the result instead of the field value.
|