Não consigo pegar um valor inteiro de uma consulta do hibernate!! Alguém pode ajudar???
public Integer getTotalInformacoes(){
Session session = MySQLDAOFactory.getSession();
Query query = session.createSQLQuery("select count(*) from informacoes");
return ((Integer) query.list().get(0)).intValue();
}
8:00:12,546 INFO BigIntegerType:182 - could not read column value from result set: ; Column '' not found.
Exception in thread "main" 08:00:12,609 WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: S0022
08:00:12,609 ERROR JDBCExceptionReporter:78 - Column '' not found.
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:2214)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
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:152)
at br.edu.fag.hello_hibernate.dao.InformacoesDAO.getTotalInformacoes(InformacoesDAO.java:54)
at br.edu.fag.hello_hibernate.Teste.main(Teste.java:16)
Caused by: java.sql.SQLException: Column '' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:961)
at com.mysql.jdbc.ResultSet.getBigDecimal(ResultSet.java:1226)
at org.hibernate.type.BigIntegerType.get(BigIntegerType.java:34)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
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:2211)
... 8 more
Ao invés de usar return ((Integer) query.list().get(0)).intValue();
Use return (Integer) query.uniqueResult();
Se não me engano o count() do Hibernate retorna um BigInteger.
D
dahenz
Otávio! Fiz o que mandaste e deu o mesmo erro!
public BigInteger getTotalInformacoes(){
Session session = MySQLDAOFactory.getSession();
Query query = session.createSQLQuery("select count(*) from informacoes");
return ((BigInteger) query.uniqueResult());
}
09:35:55,843 INFO BigIntegerType:182 - could not read column value from result set: ; Column '' not found.
09:35:55,875 WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: S0022
09:35:55,875 ERROR JDBCExceptionReporter:78 - Column '' not found.
Exception in thread "main" 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:2214)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
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:152)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:811)
at br.edu.fag.hello_hibernate.dao.InformacoesDAO.getTotalInformacoes(InformacoesDAO.java:55)
at br.edu.fag.hello_hibernate.Teste.main(Teste.java:16)
Caused by: java.sql.SQLException: Column '' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.ResultSet.findColumn(ResultSet.java:961)
at com.mysql.jdbc.ResultSet.getBigDecimal(ResultSet.java:1226)
at org.hibernate.type.BigIntegerType.get(BigIntegerType.java:34)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:189)
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:2211)
... 9 more
O que pode ser???
J
Jair_Rillo_Junior
Ao invés de BigInteger, use Integer normal.
Eu usei isso a um tempo atrás e se eu não me engano era assim mesmo
Query query = getSession().createQuery("select count(*) from ALGUMA_ENTIDADE");
return (Integer) query.uniqueResult();
A entidade relacional informacoes está sim mapeada.
O que pode ser???
Qual a diferença entre createQuery e createSQLQuery???
Obrigado
D
dahenz
Alguém conseguiu rodar uma consulta dessa forma???
Eu não consigo…
J
Jair_Rillo_Junior
createQuery é usado para criar HQL
createSQLQuery é usado para criar uma query SQL Nativa (eu acho).
Deu erro no seu mapeamento, verifique se o mesmo está correto, se está mapeado corretamente no arquivo hibernate.cfg.xml e etc.
Alias, coloque o código dos mapeamentos aqui para gente dar um olhada.
PS: eu já fiz uma consulta assim sem problemas
D
dahenz
Ow Jair... desculpa pelo incomodo... Mas estou mandando meu factory abaixo: