-->
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.  [ 3 posts ] 
Author Message
 Post subject: Problem with Column Name and Column Alias.
PostPosted: Thu Mar 24, 2011 12:00 pm 
Newbie

Joined: Thu Mar 24, 2011 11:33 am
Posts: 3
Hello,

I have a problem when i want to execute a native SQL query with JPA - Hibernate.
When some aliases are define for column, i get an error like this : "Caused by: java.sql.SQLException: Invalid column name". After some search, i found that Hibernate was sending the column name ("CODE") when the ResultSet expected to get the column alias ("aliasCode").

Here my test :
Code:
final Query query = this.entityManager.createNativeQuery("SELECT pro.CODE AS aliasCode FROM PROJECT pro WHERE pro.LIBELLE = 'Test project'", Project.class);
query.getSingleResult();

And the error i get with Oracle ojdbc6 for Oracle11g :
Code:
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not execute query
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
   at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:307)
   at mydaotest.ColumnAliasTest.testNativeSQLWithAliases(ColumnAliasTest.java:64)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
   at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110)
   at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
   at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
   at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
   at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
   at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
   at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
   at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
   at org.hibernate.loader.Loader.doList(Loader.java:2536)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
   at org.hibernate.loader.Loader.list(Loader.java:2271)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:316)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1842)
   at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:165)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:157)
   at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:274)
   ... 31 more
Caused by: java.sql.SQLException: Nom de colonne non valide
   at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3677)
   at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:2749)
   at oracle.jdbc.driver.OracleResultSet.getInt(OracleResultSet.java:434)
   at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:61)
   at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
   at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
   at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:249)
   at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:229)
   at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:330)
   at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:668)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:606)
   at org.hibernate.loader.Loader.doQuery(Loader.java:829)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
   at org.hibernate.loader.Loader.doList(Loader.java:2533)
   ... 38 more


The problem was the same for MySQL, but added a parameter ("useColumnNamesInFindColumn=true") to the jdbc.url correct it. Not a nice thing, but it works. It seem that newer JDBC fixed a bug where they were expected the column Name. But, i don't find a way to ask to Oracle JDBC to work the old way...

Do you know of a way to configure Hibernate to send the alias of the column if defined ?


Top
 Profile  
 
 Post subject: Re: Problem with Column Name and Column Alias.
PostPosted: Fri Mar 25, 2011 10:01 am 
Newbie

Joined: Thu Mar 24, 2011 11:33 am
Posts: 3
Same problem with the JDBC for PostgreSQL...

Here some link on my problem :
[url]http://postgresql.1045698.n5.nabble.com/Get-a-table-name-tt2174576.html#a2174578#none[/url]

Seems to be a problem between the JDBC 3.0 specification and the JDBC 4.0 specification.


Top
 Profile  
 
 Post subject: Re: Problem with Column Name and Column Alias.
PostPosted: Tue Mar 29, 2011 10:26 am 
Newbie

Joined: Thu Mar 24, 2011 11:33 am
Posts: 3
Same problem for SQL Server 2008 with JDBC sqljdbc4 version 3.0...
Nobody has a solution ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.