[英]JpaRepository native query: invalid table name
When I'm trying to select table by parameter:当我试图通过参数 select 表时:
@Repository
public interface IParametersRepository extends JpaRepository<Parameters, Long> {
@Query(value = "SELECT param_def_id FROM :param", nativeQuery = true)
Optional<List<String>> GetParameterDefIDs(@Param("param") String param);
}
I get invalid table name error:我收到无效的表名错误:
Hibernate: SELECT param_def_id FROM ?
2021-03-18 09:12:20.734 TRACE 980 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder :
binding parameter [1] as [VARCHAR] - [PARAM_APP]
2021-03-18 09:12:20.787 WARN 980 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper :
SQL Error: 903, SQLState: 42000
2021-03-18 09:12:20.787 ERROR 980 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper :
ORA-00903: invalid table name
But when the query is written manually I get all data from db.但是当手动编写查询时,我会从 db 获取所有数据。
@Query(value = "SELECT param_def_id FROM PARAM_APP", nativeQuery = true)
Optional<List<String>> GetParameterDefIDs(@Param("param") String param);
This is my request: localhost:8080/api/app/parameters?param=PARAM_APP
这是我的请求:
localhost:8080/api/app/parameters?param=PARAM_APP
Hello (I can't reply on your comment @SinisaT90) but to build on your answer, @RileyRiley, you could use the Custom Repository of Spring as shown here https://www.baeldung.com/spring-data-composable-repositories#3-custom-repositories您好(我无法回复您的评论@SinisaT90),但要基于您的回答,@RileyRiley,您可以使用 Spring 的自定义存储库,如下所示https://www.baeldung.com/spring-data-composable-存储库#3-custom-repositories
In other word, you could have something like that:换句话说,你可以有这样的东西:
public interface CustomRepository {
List<String> getData(String tableName);
}
and和
@Repository
public class CustomRepositoryImpl implements CustomRepository {
@PersistenceContext
private EntityManager entityManager;
@Override
public List<String> getData(String tableParam) {
String sqlQuery = "SELECT string FROM " + tableParam;
return entityManager.createQuery(sqlQuery).getResultList();
}
}
and in your service并为您服务
@Autowired
CustomRepository customRepository;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.