MySQLSyntaxErrorException with table name "condition"

java, mysql, sql

Solution

This is because condition is a reserved keyword of MySQL.

Try this:

Query qb = em.createQuery("SELECT c FROM `Condition` c ")

Check Schema Object Names for more details.

I hope it may helps you!

Problem

I have a table called `condition`. I need to keep it with that name, but when I execute the command ``` Query qb = em.createQuery("SELECT c FROM Condition c ") ``` I get this exception: ``` Caused by: 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 'condition' at line 1 at sun.reflect.GeneratedConstructorAccessor5.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2310) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:931) at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:607) ``` I know that MySQL has a command called condition and that's why this command throws me an exception. Is there a form to keep my table named as condition, and that would allow me to execute the following command? ``` Query qb = em.createQuery("SELECT c FROM Condition c ") ```

Original source