4

I am using my application since last 2-3 hrs. And suddenly getting following Exception.

Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.loader.Loader.doList(Loader.java:2216)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    at com.k2.util.HibernateQuery.execute(HibernateQuery.java:32)
    ... 107 more
 Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'EMP_INFO_TBL'.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:156)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1373)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:371)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:322)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4003)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1550)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:160)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:133)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:265)
    at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:342)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
    at org.hibernate.loader.Loader.doQuery(Loader.java:674)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2213)

This table is present in my database in proper schema(using default only i.e. dbo). All permissions are on place. Checked with connection pool which is also normal. Through JMX console I try to test database connection which is also working.

This exception even after restarting JBoss server too. Using JBoss6, JDK 6, MSSQL server. Can any one tell me what all possible reasons for "com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name"....?

user1041580
  • 209
  • 2
  • 6
  • 13
  • Make sure that the query is properly formed. Sometimes a syntax error can lead to "unhelpful" error messages. – Sebastian Meine Dec 21 '12 at 13:07
  • Were you able to find a solution to this? I am running into the same issue. I suspected that it could be due to case sensitivity in Java but I could be wrong. – rj2700 Jan 24 '17 at 16:11

4 Answers4

0

I have similar issue. The exactly same query " ...executeQuery("select * from products;"); " runs ok on RazorSQL and another query to a MySQL works ok from the same code. So it should not be an issue of wrong query.

Raimo
  • 1
0

Did something change in your environment? It's likely that you now need to specify your schema either within the xml configuration (see Problem using Hibernate and SQL Server 2008)

If you're creating sql queries within Hibernate in your sql you'll want to add your catalog(database name) and schema as well:

for example:
executeQuery("select * from products;")

would become

executeQuery("select * from yourDatabase.dbo.products;")

Community
  • 1
  • 1
Marc Johnson
  • 75
  • 1
  • 12
0

You can check your db meta data like this:

DatabaseMetaData meta = connection.getMetaData();
ResultSet res = meta.getTables(null, null, "YourTableName", new String[] {"TABLE"});
    while (res.next()) {
        System.out.println(
               "   "+res.getString("TABLE_CAT") 
               + ", "+res.getString("TABLE_SCHEM")
               + ", "+res.getString("TABLE_NAME")
               + ", "+res.getString("TABLE_TYPE")
               + ", "+res.getString("REMARKS")); 
    }

This should help to point out any obvious mistakes. Double check that your schema and other variables are indeed correct.

Chris
  • 4,593
  • 1
  • 33
  • 37
0

I faced the same error. I was connecting to a different database, which did not had the table and schema I was referring in my code.

Setting the databaseName in spring.datasource.url and spring.datasource.username to correct values fixed the error.

narendra-choudhary
  • 4,582
  • 4
  • 38
  • 58