简体   繁体   中英

JPA native query for “select into outfile”

I am having issue executing a "SELECT INTO outfile" query through JPA native query.

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. No Data.

Seems like JPA treated it as an update so I changed my code to use execute update:

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

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

Does anyone know how to fix this? Thanks!

Found a workaround. Using direct JDBC fixed the problem.

    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.

The query which you trying to execute is native one. So, if you change it to createNativeQuery might resolve an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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