PostgreSQL and Hibernate java.io.IOException: Tried to send an out-of-range integer as a 2-byte value
Asked Answered
G

4

35

I have hibernate query:

getSession()                     
        .createQuery("from Entity where id in :ids") 
        .setParameterList("ids", ids)
        .list();

where ids is Collection ids, that can contain a lot of ids. Currently I got exception when collection is very large: java.io.IOException: Tried to send an out-of-range integer as a 2-byte value I heard that postgre has some problem with it. But I can't find the solution how to rewrite it on hql.

Glenn answered 14/3, 2018 at 9:47 Comment(1)
have you solved this issue?Mendelism
B
65

The exception occurs because of PostgreSQL's limit of 32767 bind variables per statement (limit raised to 65535 since PostgreSQL driver 42.4.0). As a possible workaround try to split your large query into smaller ones.

Babu answered 10/9, 2018 at 9:52 Comment(2)
Thanks for the tips, it has saved me in a production application ! Thanks for your science @andrey-dolgikh.Shaver
related issue on GitHub github.com/pgjdbc/pgjdbc/issues/1311Scholium
B
2

In newer PostgreSQL driver 42.4.0 you can now pass up to 65,535 records.

fix: queries with up to 65535 (inclusive) parameters are supported now (previous limit was 32767) PR #2525, Issue #1311

Source: https://jdbc.postgresql.org/changelogs/2022-06-09-42.4.0-release/

Blairblaire answered 3/8, 2023 at 21:8 Comment(0)
U
-3

If you don't want split your large query into smaller ones, you may use JDBC: ResultSet executeQuery()

Utilitarianism answered 9/6, 2022 at 12:45 Comment(0)
F
-8

Try adding parenthesis around :ids, i.e.:

"from Entity where id in (:ids)"
Fremantle answered 14/3, 2018 at 10:20 Comment(7)
it does not matter. Exception the sameEldwin
then try executeUpdate();Wray
you don't know what saying, Do you know anything about work with db?Eldwin
i'm just traying to help you :)Wray
Thanks. I found that it is limits of postgreSQLEldwin
great, happy for you :)Wray
@РоманБондаренко would you mind linking to the documentation explaining those limits? I'm running into the same errorAnthozoan

© 2022 - 2025 — McMap. All rights reserved.