0

javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154) org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181) org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1617) Util.SQLControl.insertOrderLine(SQLControl.java:66) Servlet.ShoppingCardServlet.processRequest(ShoppingCardServlet.java:53) Servlet.ShoppingCardServlet.doPost(ShoppingCardServlet.java:81) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause:

org.hibernate.exception.SQLGrammarException: could not execute statement org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63) org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:178) org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:107) org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1632) org.hibernate.query.internal.NativeQueryImpl.doExecuteUpdate(NativeQueryImpl.java:295) org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1608) Util.SQLControl.insertOrderLine(SQLControl.java:66) Servlet.ShoppingCardServlet.processRequest(ShoppingCardServlet.java:53) Servlet.ShoppingCardServlet.doPost(ShoppingCardServlet.java:81) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'pepsi' in 'field list' java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) com.mysql.jdbc.Util.handleNewInstance(Util.java:406) com.mysql.jdbc.Util.getInstance(Util.java:381) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491) com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423) com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936) com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060) com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542) com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734) com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019) com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937) com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922) org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175) org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:107) org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1632) org.hibernate.query.internal.NativeQueryImpl.doExecuteUpdate(NativeQueryImpl.java:295) org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1608) Util.SQLControl.insertOrderLine(SQLControl.java:66) Servlet.ShoppingCardServlet.processRequest(ShoppingCardServlet.java:53) Servlet.ShoppingCardServlet.doPost(ShoppingCardServlet.java:81) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Note The full stack trace of the root cause is available in the server logs.

public void insertOrderLine(String orderName, String tableId, String pID, String pName , String category, int quantity, int price, String date){

       Session session = HibernateUtil.getSessionFactory().openSession();
       session.beginTransaction();
       Query query =  session.createSQLQuery("INSERT INTO "+ orderName+" VALUES ("+tableId +","+ pID+" ,"+pName +
               ","+category+","+quantity+ ","+ price+" ,"+ date+")");
        int result = query.executeUpdate();
   }
robsiemb
  • 6,157
  • 7
  • 32
  • 46

1 Answers1

0

You dont seem to be setting the columns to insert your values into! when you execute an insert query you need to specify exactly which columns to insert into, like so

INSERT INTO table([column1], [column1]) VALUES(value_for_col1, value_for_col2).

Im guessing thatws why hibernate is complaining about unknown columns. just guessing from your input parameters i imagine your query will need to look like this:

Query query =  session.createSQLQuery("INSERT INTO "+ orderName+" ([orderName], [tableId], [pID], [pName], [category], [quantity], [price], [date])VALUES ("+tableId +","+ pID+" ,"+pName +
           ","+category+","+quantity+ ","+ price+" ,"+ date+")");

Also for complex queries i suggest storing them within a sql file, this just makes editing easier and makes the code a bit neater. then just load the sql file as a string.

An even easier method to save in hibernate would just be to create an entity object, (create a class then annotate with @Entity). Hibernate will then save this object for you avoiding the need for native update queries!

Jack Branch
  • 152
  • 8
  • Thank you for the answare i try like that and the error is change in com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '[orderName], [tableId], [pID], [pName], [category], [quantity], [price], [date])' at line 1 – Stefănescu Andrei Stefan Nov 11 '19 at 15:27
  • And i can use an Entity because is about an order and the table name is dinamyc for every order – Stefănescu Andrei Stefan Nov 11 '19 at 15:28
  • ok you need to set it to autocreate the table then unless you run a migration beforehand, and i wrote query that in mssql thats probably why it didnt work!, just remove the square brakets and it should be fine: `"INSERT INTO "+ orderName+" (orderName, tableId, pID, pName, category, quantity, price, date)VALUES ("+tableId +","+ pID+" ,"+pName + ","+category+","+quantity+ ","+ price+" ,"+ date+")"` – Jack Branch Nov 11 '19 at 15:56
  • otherwise the database will not allow updating, you can actually set a parameter in spring called `ddl-autocreate` i think that will allow your entities to create a table on save [link](https://stackoverflow.com/questions/42135114/how-does-spring-jpa-hibernate-ddl-auto-property-exactly-work-in-spring) – Jack Branch Nov 11 '19 at 15:57
  • i run that query in the MySql WorkBench INSERT INTO Order_58 (tableId, pID, pName, category, quantity, price, date) VALUES (5,1 ,Cola,bar,1,2 ,11/11/2019) the error is same com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Cola' in 'field list' – Stefănescu Andrei Stefan Nov 11 '19 at 16:14
  • As far as I understand, `Cola` and `bar` are string values, so the query should be: `INSERT INTO Order_58 (tableId, pID, pName, category, quantity, price, date) VALUES (5,1,'Cola','bar' ,1,2 ,'11/11/2019' )`. Notice the quotes for non-numeric values, otherwise they're supposed to refer to other columns. – Luis Iñesta Nov 11 '19 at 17:05