javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query

Hibernate JIRA | Leonardo Penczek | 1 decade ago
  1. 0

    [HHH-2094] Hibernate is ignoring native sql aliases and generating "column not found" error - Hibernate JIRA

    atlassian.net | 12 months ago
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
  2. 0

    As simples as that: 1: Query q = em.createNativeQuery("select cd_person as codePerson, cd_person as identif from PERSON"); 2: List<Object[]> resultList = (List<Object[]>) q.getResultList(); Hibernate is ignoring my aliases when retrieving information from the ResultSet because it is trying to retrieve by column name, generating the following error: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:641) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73) at <my line 2 in the code above> ... 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:2147) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028) at org.hibernate.loader.Loader.list(Loader.java:2023) at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289) at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695) at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142) at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64) ... Caused by: java.sql.SQLException: S0022: Invalid column name 'cd_person'. at com.sybase.jdbc3.jdbc.ErrorMessage.raiseError(Unknown Source) at com.sybase.jdbc3.tds.TdsResultSet.findColumnByLabel(Unknown Source) at com.sybase.jdbc3.jdbc.SybResultSet.findColumn(Unknown Source) at com.sybase.jdbc3.jdbc.SybResultSet.getInt(Unknown Source) at org.jboss.resource.adapter.jdbc.WrappedResultSet.getInt(WrappedResultSet.java:690) at org.hibernate.type.IntegerType.get(IntegerType.java:28) at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113) at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:139) at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:474) at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:420) at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:317) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:594) at org.hibernate.loader.Loader.doQuery(Loader.java:689) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.doList(Loader.java:2144) Awesome! The SQL query runs, but the Hibernate try to get the column by the original name, not by the alias, causing the error! It is not possible to correctly get the value of 2 different columns that have the same name!!!! Hint: why Hibernate use ResultSet.get* by column name instead of positional ResultSet.get* (1, 2, 3,...)? It will solve this problem.

    Hibernate JIRA | 1 decade ago | Leonardo Penczek
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
  3. 0

    As simples as that: 1: Query q = em.createNativeQuery("select cd_person as codePerson, cd_person as identif from PERSON"); 2: List<Object[]> resultList = (List<Object[]>) q.getResultList(); Hibernate is ignoring my aliases when retrieving information from the ResultSet because it is trying to retrieve by column name, generating the following error: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:641) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73) at <my line 2 in the code above> ... 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:2147) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028) at org.hibernate.loader.Loader.list(Loader.java:2023) at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289) at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695) at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142) at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:150) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64) ... Caused by: java.sql.SQLException: S0022: Invalid column name 'cd_person'. at com.sybase.jdbc3.jdbc.ErrorMessage.raiseError(Unknown Source) at com.sybase.jdbc3.tds.TdsResultSet.findColumnByLabel(Unknown Source) at com.sybase.jdbc3.jdbc.SybResultSet.findColumn(Unknown Source) at com.sybase.jdbc3.jdbc.SybResultSet.getInt(Unknown Source) at org.jboss.resource.adapter.jdbc.WrappedResultSet.getInt(WrappedResultSet.java:690) at org.hibernate.type.IntegerType.get(IntegerType.java:28) at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113) at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:139) at org.hibernate.loader.custom.CustomLoader$ScalarResultColumnProcessor.extract(CustomLoader.java:474) at org.hibernate.loader.custom.CustomLoader$ResultRowProcessor.buildResultRow(CustomLoader.java:420) at org.hibernate.loader.custom.CustomLoader.getResultColumnOrRow(CustomLoader.java:317) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:594) at org.hibernate.loader.Loader.doQuery(Loader.java:689) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224) at org.hibernate.loader.Loader.doList(Loader.java:2144) Awesome! The SQL query runs, but the Hibernate try to get the column by the original name, not by the alias, causing the error! It is not possible to correctly get the value of 2 different columns that have the same name!!!! Hint: why Hibernate use ResultSet.get* by column name instead of positional ResultSet.get* (1, 2, 3,...)? It will solve this problem.

    Hibernate JIRA | 1 decade ago | Leonardo Penczek
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
  4. Speed up your debug routine!

    Automated exception search integrated into your IDE

  5. 0

    could not execute query in Hibernate with MySQL

    Stack Overflow | 2 years ago | Houssem Bdr
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
  6. 0

    Incorrect usage of STR() function in HQL

    Stack Overflow | 6 years ago | Stephen Watkins
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query

    9 unregistered visitors
    Not finding the right solution?
    Take a tour to get the most out of Samebug.

    Tired of useless tips?

    Automated exception search integrated into your IDE

    Root Cause Analysis

    1. javax.persistence.PersistenceException

      org.hibernate.exception.SQLGrammarException: could not execute query

      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException()
    2. Hibernate EJB
      QueryImpl.getResultList
      1. org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:641)
      2. org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73)
      2 frames