在PostgreSQL和Java的LOOP中更新

时间:2016-09-29 05:50:18

标签: java postgresql hibernate

使用Hibernate从Java应用程序更新PL / pgSQL块内的FOR循环内的表时遇到问题。

但是,当我在PostgreSQL中运行它时,SQL代码会起作用。

以下是代码:

query = session.createSQLQuery( 
"DO "+
"$do_block$ "+
"DECLARE "+
"   rec RECORD; "+
"BEGIN "+
"   FOR rec IN (select * from resultset) "+
"   LOOP "+
"   UPDATE resultset "+
"   SET c2 = tu.c2, c3 = tu.c3, c5 = tu.c5, c6 = tu.c6 "+
"   FROM "+
"   ( "+
"       SELECT rec.condition AS condition, "+
"               t2.c2 AS c2, t3.c3 AS c3, t5.c5 AS c5, t6.c6 AS c6 "+
"       FROM ( "+
"           SELECT COUNT(id) AS c2 "+ 
"           FROM "+
"               (SELECT id, firstname, middlename, lastname, birthdate, deathdate, registrationdate "+
"                   , gender, jamoatorgunit, organisationunitid, recovered "+ 
"                FROM public.cro_death "+ 
"                WHERE "+
"                   length(firstname)>= 3 "+ 
"                   AND location = 'u' "+
"                   AND gender = 'm' "+
"                   AND (jamoatorgunit IN (SELECT organisationunitid FROM tmp_ou) OR organisationunitid IN (SELECT organisationunitid FROM tmp_ou)) "+ 
"                   AND registrationDate between :startDate AND :endDate "+  
"               ) AS ti WHERE date_part('year', birthdate) BETWEEN "+
"                           CAST(substr(rec.condition, 0, strpos(LOWER(TRIM(rec.condition)),'and')-1) AS numeric) "+ 
"                               AND "+
"                           CAST(substr(LOWER(TRIM(rec.condition)), strpos(LOWER(TRIM(rec.condition)),'and')+length('and')+1, 6) AS numeric) "+ 
"           ) as t2, "+
"           ( "+
"           SELECT COUNT(id) AS c3 "+  
"           FROM "+
"               (SELECT id, firstname, middlename, lastname, birthdate, deathdate, registrationdate "+ 
"                   , gender, jamoatorgunit, organisationunitid, recovered "+ 
"                FROM public.cro_death "+ 
"                WHERE "+
"                   length(firstname)>= 3 "+ 
"                   AND location = 'u' "+
"                   AND gender = 'f' "+
"                   AND (jamoatorgunit IN (SELECT organisationunitid FROM tmp_ou) OR organisationunitid IN (SELECT organisationunitid FROM tmp_ou)) "+ 
"                   AND registrationDate between :startDate AND :endDate "+  
"               ) AS ti WHERE date_part('year', birthdate) BETWEEN "+
"                           CAST(substr(rec.condition, 0, strpos(LOWER(TRIM(rec.condition)),'and')-1) AS numeric) "+ 
"                               AND "+
"                           CAST(substr(LOWER(TRIM(rec.condition)), strpos(LOWER(TRIM(rec.condition)),'and')+length('and')+1, 6) AS numeric) "+ 
"           ) as t3, "+
"           (SELECT COUNT(id) AS c5 "+ 
"           FROM "+
"               (SELECT id, firstname, middlename, lastname, birthdate, deathdate, registrationdate "+ 
"                   , gender, jamoatorgunit, organisationunitid, recovered "+ 
"                FROM public.cro_death "+ 
"                WHERE "+
"                   length(firstname)>= 3 "+ 
"                   AND location = 'r' "+
"                   AND gender = 'm' "+
"                   AND (jamoatorgunit IN (SELECT organisationunitid FROM tmp_ou) OR organisationunitid IN (SELECT organisationunitid FROM tmp_ou)) "+
"                   AND registrationDate between :startDate AND :endDate "+
"               ) AS ti WHERE date_part('year', birthdate) BETWEEN "+
"                           CAST(substr(rec.condition, 0, strpos(LOWER(TRIM(rec.condition)),'and')-1) AS numeric) "+
"                               AND "+
"                           CAST(substr(LOWER(TRIM(rec.condition)), strpos(LOWER(TRIM(rec.condition)),'and')+length('and')+1, 6) AS numeric) "+
"           ) as t5, "+
"           (SELECT COUNT(id) AS c6 "+
"            FROM "+
"               (SELECT id, firstname, middlename, lastname, birthdate, deathdate, registrationdate "+
"                   , gender, jamoatorgunit, organisationunitid, recovered "+
"                FROM public.cro_death "+
"                WHERE "+
"                   length(firstname)>= 3 "+ 
"                   AND location = 'r' "+
"                   AND gender = 'f' "+
"                   AND (jamoatorgunit IN (SELECT organisationunitid FROM tmp_ou) OR organisationunitid IN (SELECT organisationunitid FROM tmp_ou)) "+ 
"                   AND registrationDate between :startDate AND :endDate "+
"               ) AS ti WHERE date_part('year', birthdate) BETWEEN "+
"                           CAST(substr(rec.condition, 0, strpos(LOWER(TRIM(rec.condition)),'and')-1) AS numeric) "+ 
"                               AND "+
"                           CAST(substr(LOWER(TRIM(rec.condition)), strpos(LOWER(TRIM(rec.condition)),'and')+length('and')+1, 6) AS numeric) "+ 
"           ) as t6 "+
"   )tu "+
"   WHERE resultset.condition = tu.condition; "+ 
"   END LOOP; "+
"END "+
"$do_block$ ;" 
)
.setParameter("startDate", startDate)
.setParameter("endDate", endDate) 
;

query.executeUpdate();

以下是Eclipse控制台的错误:

    * WARN  2016-09-29 10:15:05,633 SQL Error: 0, SQLState: 22023 (SqlExceptionHelper.java [qtp1983493571-23])
* ERROR 2016-09-29 10:15:05,633 The column index is out of range: 2, number of columns: 0. (SqlExceptionHelper.java [qtp1983493571-23])
* ERROR 2016-09-29 10:15:05,661 Error while executing action (ExceptionInterceptor.java [qtp1983493571-23])
org.hibernate.exception.DataException: could not execute native bulk manipulation query
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:135)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)



Caused by: org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 0.
    at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:64)
    at org.postgresql.core.v3.SimpleParameterList.setStringParameter(SimpleParameterList.java:126)
    at org.postgresql.jdbc.PgPreparedStatement.bindString(PgPreparedStatement.java:1096)
    at org.postgresql.jdbc.PgPreparedStatement.setDate(PgPreparedStatement.java:1391)
    at org.postgresql.jdbc.PgPreparedStatement.setDate(PgPreparedStatement.java:398)

0 个答案:

没有答案