JPA @query query dynamic table name

CSDN Q & A 2022-06-24 10:06:25 阅读数:68

The phenomenon and background of the problem

BFCUSTOMITEM2022 The suffix of this table is year , New tables are used every year

@Query(value = "SELECT f.CUSITEMCODE FROM BFCUSTOMITEM2022 f WHERE f.ID = ?1", nativeQuery = true)
String queryAccountingProgramCode(String id);

Operation results and error reporting contents

This is a simple test I wrote




My solution ideas and tried methods

Use table name as parameter , Use ?1 and : Parameter name Pass in the table name , But the table name will be enclosed with a pair of quotation marks when querying , Lead to SQL Report errors

What I want to achieve

2022 The suffix of will change with the year
That's true. EntityManager, Because there are too many methods involved , I don't want to change it one by one

版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。