gpt4 book ai didi

postgresql - hibernate 动态查询

转载 作者:行者123 更新时间:2023-11-29 14:02:41 26 4
gpt4 key购买 nike

我正在尝试从动态表中选择对象,但是当我运行我的代码时,我遇到了一些错误...有一种方法可以做到...我正在使用 JPA、hibernate 和 postgres

    @Query(nativeQuery = true,
value =
"SELECT u.* " +
"FROM " +
" ?1 AS u ")
Map<String, String> findAny(String tableName);

这里是错误...

org.springframework.dao.InvalidDataAccessResourceUsageException", "debugMessage": "org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialec

org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"

最佳答案

使用Hibernate,您不能将表名设置为参数,这是为了防止SQL注入(inject)等安全风险。

关于postgresql - hibernate 动态查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54317609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com