2017-07-28 12 views
0

ハイバネートセッションでネイティブSQLの削除を実行しようとしていますが、テーブルエイリアスに関する例外が発生します。私はSQLクライアントでSQLクエリを実行する場合、それは正常に動作します。私のユニットテストでスローHibernateネイティブSQLテーブルが見つかりませんでした。

String sql = 'delete c from child c join parent p on c.parent_id=p.id where p.some_id = :someId' 
SQLQuery deleteQuery = sessionFactory.currentSession.createSQLQuery(sql) 
deleteQuery.setParameter('someId', some.id.longValue()) 
deleteQuery.executeUpdate() 

例外:これは休止状態を介して動作しない理由について

[main] ERROR util.JDBCExceptionReporter - Table "C" not found; SQL statement: 

delete c from child c join parent p on c.parent_id=p.id where p.some_id = ? [42102-164] 

org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query 
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92) 
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:219) 
at org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1310) 
at org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:396) 
at org.hibernate.Query$executeUpdate.call(Unknown Source) 
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108) 
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112) 

任意の提案ですか?優れたテーブルの名前を使用して

答えて

0

(編集)

String sql = 'DELETE child FROM child INNER JOIN parent ON child.parent_id = parent.id WHERE parent.some_id = :someId'; 

これはあなたのための適応例のコード(私の)です

ADD:とエラー

... 

public boolean eliminar(Child some_id) 
{ 
    boolean result = false; 
    Session session = null; 
    Transaction rs = null; 

    try 
    { 
     session = sessionFactory.openSession(); 
     rs = session.beginTransaction(); 
     rs.setTimeout(5); 

     String query_string = "DELETE child FROM child INNER JOIN parent ON child.parent_id = parent.id WHERE parent.some_id = :someId"; 
     query_string.setParameter('someId', some_id); 
     Query q = session.createQuery(query_string); 
     q.executeUpdate(); 

     rs.commit(); 

     result = true; 
    } 
    catch(RuntimeException e) 
    { 
     try 
     { 
      rs.rollback(); 
     } 
     catch(RuntimeException rbe) 
     { 
      System.out.println(rbe.getMessage()); 
     } 
     System.out.println(e.getMessage()); 
    } 
    finally 
    { 
     session.close(); 
    } 
    return result; 
} 
... 
+0

:ERRORのutil.JDBCExceptionReporter - SQLステートメントの構文エラーが「子どもから削除c join [*] parent p on c.parent_id = p.idここで、p.some_id =? "; SQLステートメント: 子からの削除c親pのc.parent_id = p.idの場合p.some_id =? [42000-164] – shuttsy

+0

私は自分のエントリーを編集し、新しいクエリーでやり直してください。私はテーブルの名前のエイリアスを置き換えます。エラーは、mysqlがテーブルに一致しないことです。 – MikeSouto

+0

'from'部分に構文エラーがあります – shuttsy

関連する問題