select into outfile不能使用hibernate

时间:2013-09-10 09:58:04

标签: java hibernate

我正在尝试在Hibernate中执行de follow代码以从mySQL数据库创建.csv文件。 :

String sql = 
"SELECT * INTO OUTFILE 'table.csv' FIELDS TERMINATED BY ','" +
" OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' " +
"FROM match INNER JOIN totala ON match_code= match";

.csv文件已正确创建,但后来出现以下错误:

Exception monitoring the input folder org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2452)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192)
    at org.hibernate.loader.Loader.list(Loader.java:2187)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1832)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:179)
    at com.salebuild.dao.hibernate.CorporateTitleDAOImpl.dumpTitleCompanyCSV(CorporateTitleDAOImpl.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
    at $Proxy128.dumpTitleCompanyCSV(Unknown Source)
    at com.salebuild.service.CorporateTitleServiceImpl.dumpTitleCompanyCSV(CorporateTitleServiceImpl.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at $Proxy159.dumpTitleCompanyCSV(Unknown Source)
    at com.salebuild.service.collector.ContactScraperMonitorInputFolder.addNewFile(ContactScraperMonitorInputFolder.java:61)
    at com.salebuild.service.collector.ContactScraperMonitorInputFolder.run(ContactScraperMonitorInputFolder.java:37)
    at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: ResultSet is from UPDATE. No Data.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
    at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7021)
    at com.mchange.v2.c3p0.impl.NewProxyResultSet.next(NewProxyResultSet.java:2859)
    at org.hibernate.loader.Loader.doQuery(Loader.java:741)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    ... 29 more

有些谷歌搜索建议可能是因为查询没有返回任何结果所以我得到“java.sql.SQLException:ResultSet来自UPDATE。没有数据”。但是我如何克服这个异常。我不希望我的代码破坏。

我正在运行查询:

public void dumpTitleCompanyCSV(String titleId,String titleName, String dumpFolder) {
        Session session = getSession();
        String hql = "SELECT "+titleId+" ,id, \""+titleName+"\", name " +
                    "FROM company INTO OUTFILE 'd:/contact/input/file.csv' " +
                    "FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'";
        logger.debug("jeets The query is \n "+hql);
        Query query  = session.createSQLQuery(hql);
         query.list();
    }

1 个答案:

答案 0 :(得分:2)

使用executeUpdate()方法运行不会返回任何结果的查询。

在您的情况下,由于结果将被转储到外部文件,因此将不会设置结果。请注意,这与由于未满足条件而可能返回空结果的查询不同。

编辑:

由于这似乎也失败了,我认为您可能需要尝试使用纯JDBC。

执行以下操作

session.doWork(new Work() {
    @Override
    public void execute(Connection con) throws SQLException {
        //run your query natively here
    }
});