gpt4 book ai didi

Java Hibernate设置参数

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:18 25 4
gpt4 key购买 nike

我试图使用 Hibernate 执行一个简单的查询,但是它给出了错误。我在网络上搜索,参数传递语法似乎是正确的,但不知道为什么会发生这种情况。

代码

 Session session = analyticsSQLSession.getSession();

try {
session.beginTransaction();
Query query = session.createNativeQuery("select * from :tableName");
query.setParameter("tableName", tableName);

List resultList = query.getResultList();
session.close();
System.out.println();

return resultList;
} catch (Exception ex) {
throw ex;
} finally {
session.close();

错误

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

最佳答案

在这里,你必须执行类似这样的操作,尝试这个并让我知道

 Session session = analyticsSQLSession.getSession();

try {
session.beginTransaction();
Query query = session.createNativeQuery(tableName("tableName"));
//query.setParameter("tableName", tableName);


List resultList = query.getResultList();
session.close();
System.out.println();

return resultList;
} catch (Exception ex) {
throw ex;
} finally {
session.close();



public static String tableName(String tableName) {
return "SELECT * FROM " + tableName;
}

关于Java Hibernate设置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59731583/

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