2015-09-12 227 views
0

我試圖執行休眠插入SQL查詢休眠插入不工作

String sqlQuery = "insert into examschedule (class_id,examtype_id,subject_id,examdate,start_time,end_time,messagetype_id,createddate) values"; 
    String sql1 =sql1+ "("+ classes.getId()+","+ 
          exType.getId()+","+ 
          subjectName.getId()+","+ 
          "'"+sqlDate+"'"+","+"'"+startTime+"'"+","+"'"+endTime+"'"+","+msgType.getId()+","+"'"+sqlTimestamp+"'"+");"; 

System.out.println(sqlQuery+sql1); 
       query = session.createSQLQuery(sqlQuery); 
       int result = query.executeUpdate(); 

SQL語句,但是,我得到異常下面當我試圖執行在MySQL控制檯相同的查詢,還有就是它完美地執行沒有語法錯誤

爲什麼在我調用createSQLQuery())查詢對象後,值參數會被刪除。

18:20:16,855 INFO [stdout] (http-localhost-127.0.0.1-8080-2) insert into examschedule (class_id,examtype_id,subject_id,examdate,start_time,end_time,messagetype_id,createddate) values(13,1,5,'2015-09-07','11AM','12PM',2,'2015-09-12 18:20:16.855'); 
18:20:16,868 INFO [stdout] (http-localhost-127.0.0.1-8080-2) Hibernate: insert into examschedule (class_id,examtype_id,subject_id,examdate,start_time,end_time,messagetype_id,createddate) values 

18:20:16,871 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-2) SQL Error: 1064, SQLState: 42000 
18:20:16,871 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-2) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 
18:20:16,876 ERROR [stderr] (http-localhost-127.0.0.1-8080-2) org.hibernate.exception.SQLGrammarException: could not execute statement 
+2

如果你只打算通過連接字符串來構建一個查詢,爲什麼還要打擾hibernate呢? – e4c5

回答

0

你在你的代碼我我提的意見有些錯誤:

String sqlQuery = "insert into examschedule (class_id,examtype_id,subject_id,examdate,start_time,end_time,messagetype_id,createddate) values"; 
    String sql1 =sql1+ "("+ classes.getId()+","+ 
       //^^ here you are adding undefined value remove it 
          exType.getId()+","+ 
          subjectName.getId()+","+ 
          "'"+sqlDate+"'"+","+"'"+startTime+"'"+","+"'"+endTime+"'"+","+msgType.getId()+","+"'"+sqlTimestamp+"'"+");"; 

System.out.println(sqlQuery+sql1); //complete query is in sqlQuery+sql1 
query = session.createSQLQuery(sqlQuery); //here you are passing half query use sqlQuery+sql1 
int result = query.executeUpdate(); 

考慮和嘗試下面的代碼片段:

String sqlQuery = "insert into examschedule (class_id,examtype_id,subject_id,examdate,start_time,end_time,messagetype_id,createddate) values"; 
    sqlQuery = sqlQuery + "("+ classes.getId()+","+ 
          exType.getId()+","+ 
          subjectName.getId()+","+ 
          "'"+sqlDate+"'"+","+"'"+startTime+"'"+","+"'"+endTime+"'"+","+msgType.getId()+","+"'"+sqlTimestamp+"'"+");"; 

System.out.println(sqlQuery); 
query = session.createSQLQuery(sqlQuery); 
int result = query.executeUpdate(); 

的調試運行,請不要給SO你的片段。閱讀What types of questions should I avoid asking?

+0

謝謝,它的工作,但是,我不明白我到底犯了什麼錯誤 –

+0

你的錯誤是你在查詢中傳遞'sqlQuery'而不是'sqlQuery + sql1' – Manwal

0

您可以嘗試直接在數據庫中執行sql語句。它會給你任何查詢錯誤。問題可能像在表名之前添加模式名稱。