gpt4 book ai didi

java - JPA native 查询 "select into outfile"

转载 作者:行者123 更新时间:2023-11-29 22:00:29 28 4
gpt4 key购买 nike

我在通过 JPA native 查询执行“SELECT INTO outfile”查询时遇到问题。

我尝试使用以下代码:

Query q = entityManager.createNativeQuery("SELECT * INTO OUTFILE '/tmp/temp.sql' FROM DummyTable");
return q.getResultList();

但是我遇到了这个异常:

Caused by: java.sql.SQLException: ResultSet is from UPDATE. No Data.

似乎 JPA 将其视为更新,因此我更改了代码以使用执行更新:

Query q = entityManager.createNativeQuery("SELECT * INTO OUTFILE '/tmp/temp.sql' FROM DummyTable");
q.executeUpdate();

现在,我遇到了另一个异常。

Caused by: org.hibernate.exception.GenericJDBCException: could not execute native bulk manipulation query

Caused by: java.sql.SQLException: Can not issue executeUpdate() for SELECTs

有人知道如何解决这个问题吗?谢谢!

最佳答案

找到了解决方法。使用直接 JDBC 解决了该问题。

    Session session = (Session) entityManager.getDelegate();
session.doWork(new Work() {

@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement pStmt = null;
try {
pStmt = connection.prepareStatement(query);
pStmt.execute();
} finally {
if (pStmt != null) {
pStmt.close();
}
}

}
});

关于java - JPA native 查询 "select into outfile",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707153/

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