2016-11-21 6 views
0

私のWebプロジェクトでhibernateを使用しています。そして、Stringのこの行をcreateSQLQuery(queryString)に実行しています。今、私はcreateSQLQuery(queryString).executeUpdate();でそれを実行したとき1つの文字列で多くのクエリを操作できる休止状態にはありますか?

DROP TABLE monthlyevaluatedbudgettabletemp; 
DROP TABLE monthlyadjustedbudgettabletemp; 

CREATE TABLE monthlyevaluatedbudgettabletemp (budgetid bigint NOT NULL, 
    approvedbudget numeric(19,2), 
    april numeric(19,2), 
    august numeric(19,2), 
    budgetforyear bigint NOT NULL, 
    december numeric(19,2), 
    february numeric(19,2), 
    january numeric(19,2), 
    july numeric(19,2), 
    june numeric(19,2), 
    march numeric(19,2), 
    may numeric(19,2), 
    november numeric(19,2), 
    october numeric(19,2), 
    september numeric(19,2), 
    branchid integer, 
    accountid bigint, 
    budgetlastyear bigint); 

CREATE TABLE monthlyadjustedbudgettabletemp (adjustedid bigint NOT NULL, 
    april numeric(19,2), 
    august numeric(19,2), 
    december numeric(19,2), 
    february numeric(19,2), 
    january numeric(19,2), 
    july numeric(19,2), 
    june numeric(19,2), 
    march numeric(19,2), 
    may numeric(19,2), 
    november numeric(19,2), 
    october numeric(19,2), 
    september numeric(19,2), 
    yeartodatemonth character varying(255), 
    budgetid bigint); 

INSERT INTO monthlyevaluatedbudgettabletemp SELECT * FROM monthlyevaluatedbudgettable where budgetforyear=2017; 
INSERT INTO monthlyadjustedbudgettabletemp SELECT a.* from monthlyadjustedbudgettable as a join monthlyevaluatedbudgettable as e on a.budgetid=e.budgetid where e.budgetforyear = 2017 

UPDATE monthlyevaluatedbudgettabletemp SET budgetforyear=2017,budgetlastyear=2016,april=0.00,approvedbudget=0.00,august=0.00,february=0.00,january=0.00,july=0.00,june=0.00,march=0.00,may=0.00,november=0.00,october=0.00,september=0.00,december=0.00; 

UPDATE monthlyadjustedbudgettabletemp SET january=0.00,february=0.00,march=0.00,april=0.00,may=0.00,june=0.00,july=0.00,august=0.00,september=0.00,november=0.00, 
october=0.00,december=0.00; 

:以下私はqueryStringに割り当てられた値です。この種のエラーが発生しました。

org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query 

私の質問では、ワンショットの文字列でクエリを操作できる方法があります。または各クエリ文を分割して1つずつ実行する必要がありますか。 hibernateで.sqlファイルを実行したいのと同じです。

+2

[Hibernate複数のネイティブSQL文](http://stackoverflow.com/questions/15312697/hibernate-multiple-native-sql-statements) これらのクエリを1つずつ実行する必要があると思います。 – Pika

+1

なぜこのような種類の操作にhibernateを使用するのですか? JDBCを使用してください –

答えて

2

Hibernateで複数のネイティブクエリを実行することはできません。

クエリを分割して個別に実行する必要があります。

これはJDBCでも同様です。

1

私は他の人と共有するためにこの種のコードを考え出しました。しかし、私は素敵な答えを与えてくれた@Pritam Banerjeeの答えをチェックします。私はsplit私のStringで ";"それぞれの文を区切るためにシンボルを使用しています。私はloopを使用して1つずつ実行します。

String[] splitQueryStr = queryString.split(";"); 
    for (String queryStr : splitQueryStr) { 
     try { 
      getSession().beginTransaction(); 
      getSession().createSQLQuery(queryStr).executeUpdate(); 
      getSession().getTransaction().commit(); 
     } catch (HibernateException he) { 
      getSession().getTransaction().rollback(); 
      he.printStackTrace(); 
      throw new HibernateException("Hibernate Error"); 
     } catch (Exception e) { 
      getSession().getTransaction().rollback(); 
      throw new Exception(); 
     } 
    } 

私はうまく動作します。コメントと答えをありがとう。感謝します。

関連する問題