'Spring Data JPA Named Native Queries pass Dynamic table name
I have repeated select query with exact same column but different Table/View name. Is there any option to reduce the number of lines from my orm.xml file?
Example:
<named-native-query name="PeriodEntity.periodQuery.NA"
result-class="com.data.store.foundation.model.PeriodEntity">
<query>
SELECT
LEVELNAME,
TIME_SVC_LVL_OPR_DESC,
TIME_SVC_LVL_NBR,
CALENDARID,
CHANNEL_KEY,
ISCURRENT,
PREVIOUS_CALENDARID,
START_DT,
ENDDATE,
CXT,
CAL_YEAR,
ID,
RANGE_DATE,
LBL,
SHORT_LABEL,
FISCAL_YEAR
FROM
dbo.v_NRT_Time_Hier_RTL_FRCH
WHERE
Channel_key = :channelId
</query>
</named-native-query>
<named-native-query name="PeriodEntity.periodQuery.EU"
result-class="com.data.store.foundation.model.PeriodEntity">
<query>
SELECT
LEVELNAME,
TIME_SVC_LVL_OPR_DESC,
TIME_SVC_LVL_NBR,
CALENDARID,
CHANNEL_KEY,
ISCURRENT,
PREVIOUS_CALENDARID,
START_DT,
ENDDATE,
CXT,
CAL_YEAR,
ID,
RANGE_DATE,
LBL,
SHORT_LABEL,
FISCAL_YEAR
FROM
dbo.v_Day_Time_Hier_RTL
WHERE
Channel_key = :channelId
</query>
</named-native-query>
Note:
- We are selecting only required columns from the table/view.
- Can we declare a variable for all columns and use a variable name in the select query?
- Can we provide the table/view name dynamically? So single <named-native-query name="PeriodEntity.periodQuery.Generic" will work for both?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
