'Need to add variable in @Query
I want to pass a date as a variable to my table name in the query as the table is going to be created on the daily basis.
`@Repository public interface SpeedDataRepo extends JpaRepository<SpeedData, String> {
final static String date= DateTimeFormat.newDate();
@Query(value= "select prd.\"Lat\", prd.\"Lon\", prd.\"Location\", prd.\"Heading\" as direction, prd.\"StringDateTime\", prd.\"Speed\" "
+ "from \"VTS_AIS140\".\"ProcessedRawData_"+date+"\" prd "
+ "where prd.\"VehicleRegNo\"=:vehicle order by \"StringDateTime\" desc limit 15\"", nativeQuery=true)
public Object[][] findByVehicle(@Param("vehicle")String vehicle);
}`
As you can see the table name with ProcessedRawData will be created on daily basis so the table name should be like ProcessedRawData_20220316. So I made a method to get the current date and pass it after ProcessedRawData_+date+.
But when I use it like this it gives me an error saying:
"The value for annotation attribute Query.value must be a constant expression"
As the date is going to change every time I think its asking for constant value that can't be changed.
Is there a way to deal with this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
