How to replace table name with value from parameter while using Spring Data JPA nativeQuery

给你一囗甜甜゛ 提交于 2019-12-17 17:22:49

问题


like this:

public interface XXXRepository extends CrudRepository<XXX, Integer> {
@Query(value = "select * from ?1 where ...", nativeQuery = true)
List<XXX> findByXXX(String tableName, ...);}

It gives MYSQL syntax error with upon codes. The syntax error shows that the table name in the SQL is surrounded with "'".


回答1:


This is not possible. Parameters are only allowed in the where clause.




回答2:


You can use entitymanger in jpa project.

@Autowired EntityManager entityManager;

entityManager.createNativeQuery("select * from "+tableName+"")



来源:https://stackoverflow.com/questions/40956013/how-to-replace-table-name-with-value-from-parameter-while-using-spring-data-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!