Get results as CSV from postgresql using sleep mode

I execute a query that should return results in CSV in STDOUT.

When I execute my request in pgAdmin, I successfully get the results.

However, when I execute the same query using hibernate, I get the following exception:

javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Failed to retrieve ResultSet

I should not show the structure of the tables, but I know that sql is fine (I copied all the contents of "sql", then executed it in pgAdmin); The request looks like this:

String sql = "COPY (" + sqlQuery + ") TO STDOUT WITH CSV";

Then I execute it like the following:

Query query = getEntityManager().createNativeQuery(sql);

Object result = query.getSingleResult(); // I also tried the other get results method...(`getFirstresult()` has returned 0)

In any related questions I found, I saw that OP puts csv in a file instead of stdout.

Is it possible to return csv result using hibernate?

Thanks in advance!

+4
2

AFAIK, COPY PostgreSQL JDBC ( postgresql-9.4.1208.jre7). , Hibernate .

COPY, CopyManager: PostgreSQL JDBC?

. COPY .

+1

univocity-parsers . :

CsvRoutines routines = new CsvRoutines();
routines.write(resultset, new File("/path/to/output.csv"), "UTF-8");

write() . .

0

Source: https://habr.com/ru/post/1658209/


All Articles