简体   繁体   English

JPA本机查询“选择入文件”

[英]JPA native query for “select into outfile”

I am having issue executing a "SELECT INTO outfile" query through JPA native query. 我在通过JPA本机查询执行“ SELECT INTO外文件”查询时遇到问题。

I tried with the following code: 我尝试使用以下代码:

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

But I got this exception: 但是我有一个例外:

Caused by: java.sql.SQLException: ResultSet is from UPDATE. 引起原因:java.sql.SQLException:ResultSet来自UPDATE。 No Data. 没有数据。

Seems like JPA treated it as an update so I changed my code to use execute update: 似乎JPA将其视为更新,因此我将代码更改为使用执行更新:

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

Now, I got another exception. 现在,我又有一个例外。

Caused by: org.hibernate.exception.GenericJDBCException: could not execute native bulk manipulation query 原因:org.hibernate.exception.GenericJDBCException:无法执行本机批量操作查询

Caused by: java.sql.SQLException: Can not issue executeUpdate() for SELECTs 原因:java.sql.SQLException:无法为SELECTs发出executeUpdate()

Does anyone know how to fix this? 有谁知道如何解决这一问题? Thanks! 谢谢!

Found a workaround. 找到了解决方法。 Using direct JDBC fixed the problem. 使用直接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();
                }                       
            }

        }
    });

EntityManager.createQuery creates JPQL query interface whereas EntityManager.createNativeQuery will create native query interface. EntityManager.createQuery创建JPQL查询接口,而EntityManager.createNativeQuery将创建本机查询接口。

The query which you trying to execute is native one. 您尝试执行的查询是本机查询。 So, if you change it to createNativeQuery might resolve an issue. 因此,如果将其更改为createNativeQuery可能会解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM