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();
   }
2
  • Read the stacktrace.... Unknown column 'pepsi' in 'field list' Commented Nov 11, 2019 at 15:16
  • Yes the pepsi is value get on another table and signed in pName column Commented Nov 11, 2019 at 15:18

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

6 Comments

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
And i can use an Entity because is about an order and the table name is dinamyc for every order
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+")"
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
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'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.