Hibernate插入SQL语句不起作用

时间:2015-09-12 13:11:42

标签: java mysql hibernate

我正在尝试执行hibernate insert sql query

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();

但是,当我在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 个答案:

答案 0 :(得分: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?

答案 1 :(得分:0)

您可以直接尝试在数据库中执行sql语句。它将为您提供查询的任何错误。问题可能就像在表名之前添加模式名称一样。