[Solved]com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘ABC’ doesn’t exist

“com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘test.stock’ doesn’t exist” this exception in hibernate generally occured when mentioned table in mapping(hbm.xml) or JPA annotated entity table not exist in database. Sometime table exist in database but table with mentioned catalog or schema (due to copy paste or typing mistake) doesn’t exist.

Problem

In this case issue is “com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table ‘test.stock’ doesn’t exist”. This issue is because of ‘test.stock’ table is not find out because here in put mentioned catalog as “test” that is schema or database name while in hibernate.cfg.xml mentioned schema name as facingissuesonitdb.

Input Stock.hbm.xml


<hibernate-mapping>
    <class name="com.facingissuesonit.model.Stock" table="stock" catalog="test">
    <id name="stockId" type="java.lang.Integer">
            <column name="STOCK_ID" />
            <generator class="identity" />
        </id>
        <property name="stockCode" type="string">
            <column name="STOCK_CODE" length="10" not-null="true" unique="true" />
        </property>
        <property name="stockName" type="string">
            <column name="STOCK_NAME" length="20" not-null="true" unique="true" />
        </property>
     
    </class>
</hibernate-mapping>
     

Exception StackTrace


ERROR: Table 'test.stock' doesn't exist
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.stock' doesn't exist
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2643)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:384)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:205)
    ... 22 more

Solutions

  • Solution 1: Replace above xml attribute catalog value from “test” to “facingissuesonitdb” as used same in hibernate.cfg.xml.
  • Solution 2: If your application is using only one database/schema then recommendation is to mention schema name in hibernate.cfg.xml so that in future if your schema name get change only configuration file need to update not all mapping files.
    
    <property name="hibernate.default_schema">'facingissuesonitdb'</property>