-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Hibernate is generating invalid queries
PostPosted: Mon Aug 31, 2009 7:56 am 
Newbie

Joined: Wed Jun 06, 2007 9:14 am
Posts: 14
- Hibernate 3.3.1.GA
- Hibernate Annotations 3.4.0.GA
- Hibernate EntityManager 3.4.0.GA
- MS SQL Server 2000

Hello, everybody!

I'm upgrading from Hibernate 3.2.4.sp1 to Hibernate 3.3.1.GA . Now, when I execute
the following query (that worked in Hibernate 3.2.4.sp1 ):

Code:
String nome = (String) em.createQuery(
    "SELECT u.nome " +
    "FROM Funcionario u " +
    "WHERE u.codigo = :codigoFuncionario AND " +
    "   (u.situacao IN ('A', 'C', 'D', 'T') OR u.situacao IS NULL)").
    setParameter("codigoFuncionario", codigoFuncionario).
    getSingleResult();

I get this error:

Code:
08:19:49,875 WARN  [JDBCExceptionReporter] SQL Error: 170, SQLState: S0001
08:19:49,906 ERROR [JDBCExceptionReporter] Line 1: Incorrect syntax near '2'.
08:19:49,906 ERROR [AbstractEntityManagerImpl] Unable to mark for rollback on PersistenceException:
java.lang.IllegalStateException: [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] [com.arjuna.ats.internal.jta.transaction.arjunacore.nosuchtx] No such transaction!
   at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.setRollbackOnly(BaseTransaction.java:191)
   at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.setRollbackOnly(BaseTransactionManagerDelegate.java:123)
   at org.hibernate.ejb.AbstractEntityManagerImpl.markAsRollback(AbstractEntityManagerImpl.java:421)
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:576)
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
   at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:128)
   at br.urca.solicitacoes.implementacao.LocalizadorFuncionariosBean.localizarNomeFuncionario(LocalizadorFuncionariosBean.java:64)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
   at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_2523263.invoke(InvocationContextInterceptor_z_fillMethod_2523263.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_2523263.invoke(InvocationContextInterceptor_z_setup_2523263.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
   at org.jboss.ejb3.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:114)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptorv2.invoke(RoleBasedAuthorizationInterceptorv2.java:201)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:176)
   at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:216)
   at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
   at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
   at $Proxy321.localizarNomeFuncionario(Unknown Source)
   at br.urca.solicitacoes.web.AjaxServlet.localizarNomeFuncionario(AjaxServlet.java:64)
   at br.urca.solicitacoes.web.AjaxServlet.doGet(AjaxServlet.java:33)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
   at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
   at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Unknown Source)
08:19:49,984 ERROR [[Ajax Servlet]] Servlet.service() for servlet Ajax Servlet threw exception
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
   at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:128)
   at br.urca.solicitacoes.implementacao.LocalizadorFuncionariosBean.localizarNomeFuncionario(LocalizadorFuncionariosBean.java:64)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
   at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
   at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_2523263.invoke(InvocationContextInterceptor_z_fillMethod_2523263.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
   at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_2523263.invoke(InvocationContextInterceptor_z_setup_2523263.java)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPolicy.invokeInNoTx(TxPolicy.java:66)
   at org.jboss.ejb3.tx.TxInterceptor$NotSupported.invoke(TxInterceptor.java:114)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.RoleBasedAuthorizationInterceptorv2.invoke(RoleBasedAuthorizationInterceptorv2.java:201)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
   at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:176)
   at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:216)
   at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
   at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
   at $Proxy321.localizarNomeFuncionario(Unknown Source)
   at br.urca.solicitacoes.web.AjaxServlet.localizarNomeFuncionario(AjaxServlet.java:64)
   at br.urca.solicitacoes.web.AjaxServlet.doGet(AjaxServlet.java:33)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
   at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
   at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.loader.Loader.doList(Loader.java:2231)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
   at org.hibernate.loader.Loader.list(Loader.java:2120)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
   at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:88)
   ... 79 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: Incorrect syntax near '2'.
   at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
   at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
   at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(Unknown Source)
   at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:342)
   at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
   at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
   at org.hibernate.loader.Loader.doQuery(Loader.java:697)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
   at org.hibernate.loader.Loader.doList(Loader.java:2228)
   ... 87 more


Let me tell you what is happening. The database that I'm using in SQL Server 2000
has a compatibility level of 65 (which means it is compatible with SQL Server 6.5).
This particular compatibility level doesn't accept queries of the type
SELECT TOP <number of records> , but this version of Hibernate is generating a
query in this style for the JPQL query above. That's why you can see messages like
'SQL Error: 170, SQLState: S0001' and 'Line 1: Incorrect syntax near '2'' in the
error. So, Hibernate is not considering the database compatibility level and is
generating invalid queries.

To fix this problem I could just change the compatibility level in the database
for one higher (say, for example, 80), but I remember that we already did that
in the past and unfortunately all kind of strange errors started to appear in
our database. So, I can't just change the compatibility level.

This is clearly a case that Hibernate could have handled automatically. I there
isn't a API that provide this information to Hibernate, it could simple execute
a dummy query under the hood with the TOP keyword. If it get the error, it would
know that queries like this are not supported in the database being used.

So, I would like to know if there's a solution or a workaround to this problem.
There should be a way to instruct Hibernate not to generate queries with the TOP
keyword. Hibernate should not force me to use native queries in this case.

Marcos


Top
 Profile  
 
 Post subject: Re: Hibernate is generating invalid queries
PostPosted: Wed Sep 02, 2009 8:49 am 
Newbie

Joined: Wed Jun 06, 2007 9:14 am
Posts: 14
This is really an important issue. Hibernate is clearly doing something wrong.
It is ignoring an important, not an irrelevant, database feature that has
impact on the generated SQL, which means that an ORM solution can generate
invalid queries if it doesn't consider it, what is happening in this case.

Hibernate changed its implementation in some way in a new version that broke
existing clients. Hibernate can't assume that everyone that is using a database
created in MS SQL Server 2000 is with the database configured with a compatibility
level that allows the TOP keyword in a SELECT statement
. I'm not saying that
Hibernate don't have to change its implementation, I'm just saying that it must
not change its implementation in a way that break existing clients, or otherwise
provide a configuration for this.

Marcos


Top
 Profile  
 
 Post subject: Re: Hibernate is generating invalid queries
PostPosted: Wed Sep 02, 2009 11:30 am 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi Marcos,
more questions to you..
1)What kind of driver do you use?
2)We use SQL Server 2005 so I don't know the default compatibility level for SQL Server2000.
3)Can you post the generated SQL?
4)can you get rid of the method getSingleResult() and that does not help you? or may be let me ask you this.. can you change the Java end of your app?

Sorry more questions than any answer to you.
Good luck!
Srilatha.


Top
 Profile  
 
 Post subject: Re: Hibernate is generating invalid queries
PostPosted: Wed Sep 02, 2009 12:56 pm 
Newbie

Joined: Wed Jun 06, 2007 9:14 am
Posts: 14
Hello, latha1119.

Here are the answers to your questions:

1)What kind of driver do you use?

I'm using the official Microsoft SQL Server 2005 JDBC Driver 1.2
(which is compatible with Microsoft SQL Server 2000)

2)We use SQL Server 2005 so I don't know the default compatibility level for SQL Server2000.

The default database compatibility level for SQL Server 2000 is 80, which accepts
to TOP keyword in SELECT statements. But as I said, we are using a database with
compatibility level 65. This is an old database here in the company where I work
that was converted from SQL Server 7, I suppose. So, it came with the compatibility
level unchanged, I think. As I also said we tried changing the compatibility level
to 80, but all sort of strange errors appeared in the applications. So, we reverted
to the old configuration and the errors went away.

3)Can you post the generated SQL?

Unfortunately I can't right now. I noticed that JBoss 5.1.0.GA (that I'm migrating for)
doesn't log the Hibernate messages in the server.log file anymore. I suppose there
should be a way to achieve this, but I haven't tried to find out how yet. Nevertheless,
I'm sure about the genereted SQL the Hibernate genereted in this case. If you open the
SQL Query Analyzer in SQL Server and execute this following query in a database with
compatibility level 80:

SELECT TOP 2 1

the query will work and will return one row with one column with the value 1. But if
you execute the same query in a database that has compatibility level 65, for example,
you will get this error message in the SQL Query Analyzer:

Code:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '2'.


It clearly doesn't understand the TOP keyword. Now compare this message error with the
relevant parts of the error message in the server.log in JBoss that I showed you in
the beginning of this thread:

Code:
08:19:49,875 WARN  [JDBCExceptionReporter] SQL Error: 170, SQLState: S0001
08:19:49,906 ERROR [JDBCExceptionReporter] Line 1: Incorrect syntax near '2'.

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: Incorrect syntax near '2'.


Now you know Hibernate is generating a SQL with the TOP keyword, which didn't happen
in Hibernate 3.2.4.sp1.

4)can you get rid of the method getSingleResult() and that does not help you?
or may be let me ask you this.. can you change the Java end of your app?


If I get rid of the method getSingleResult() and replace it with getResultList()
I'm almost sure the error will disappear. But now I'll have to deal with the
result list just to get one value or one record in a case I know there's only
one result. I'll have to change all code the calls getSingleResult(). This in
without doubt an ugly workaround that will make the code apper non intuitive and
longer that it would should be.

Marcos


Top
 Profile  
 
 Post subject: Re: Hibernate is generating invalid queries
PostPosted: Wed Sep 02, 2009 1:36 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi Marcos,
I know you almost hit a dead end.. but changing the driver helps? have you tried JTDS driver (from net.sourceforge.jtds) at all? As I mentioned earlier we use SQL Server 2005 and even in our earlier app where we had SQL Server 2000 we have used the JTDS driver.
Hope this helps,
Srilatha.


Top
 Profile  
 
 Post subject: Re: Hibernate is generating invalid queries
PostPosted: Wed Sep 02, 2009 1:48 pm 
Newbie

Joined: Wed Jun 06, 2007 9:14 am
Posts: 14
latha1119, changing the driver won't help in this case. The problem is in the SQL
generated by Hibernate, not in the driver itself. So, even I if change the driver
Hibernate will continue generating the same SQL. Besides, I don't want to change
the driver because I'm very satisfied with it. It's fast and haven't shown any bug
until now.

Marcos


Top
 Profile  
 
 Post subject: Re: Hibernate is generating invalid queries
PostPosted: Fri Sep 04, 2009 8:24 am 
Newbie

Joined: Wed Jun 06, 2007 9:14 am
Posts: 14
I changed the method implementation to this:

Code:
public String localizarNomeFuncionario(int codigoFuncionario)
{
    List<String> nomes = (List<String>) getGE().createQuery(
        "SELECT u.nome " +
        "FROM Funcionario u " +
        "WHERE u.codigo = :codigoFuncionario").
        setParameter("codigoFuncionario", codigoFuncionario).
        getResultList();
    String nome = nomes.isEmpty() ? null : nomes.get(0);
    return nome;
}

Now the error is gone. You can see that this is a workaround. As suspected,
changing from getSingleResult to getResultList don't raise the error, but
now I have to test the resulting list to see if it is empty before getting
the value. So, when we use getSingleResult Hibernate generates a SQL with
the TOP keyword even though the database compatibility level being used doesn't
support it.

So, Hibernate is throwing this responsability in the developer when it should
have handled it automatically. The resulting code for this workaround is less
intuitive, more error prone and slower, because a list is being created when
it shouldn't.

Marcos


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.