[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2094) Hibernate is ignoring native sql aliases and generating "column not found" error
shotsheep (JIRA)
noreply at atlassian.com
Wed Jan 20 21:25:29 EST 2010
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2094?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=35286#action_35286 ]
shotsheep commented on HHH-2094:
--------------------------------
It's so late i saw this case, and i also has this problem, hope follows help anyone:
in jpa, althoght it's a native SQL, you can map result as a entity , but if you don't want to do it, you can just tells jpa what columns will be return or a composition of entities and columns .
see example :
String strSql = "select cd_person as codePerson, cd_person as identif from PERSON";
you can define a SqlResultSetMapping in any @Entity to tells return columns and entity:
1
@SqlResultSetMapping(name="PersonRetMapping",
columns={ @ColumnResult(name="codePerson"),
@ColumnResult(name="identif")
}
)
2
then in the qurey
EntityManager em = emf.createEntityManager();
Query q = em.createNativeQuery(strSql , "PersonRetMapping");
List<Object[]> lst = q.getResultList();
for (Iterator<Object[]> i = lst.iterator(); i.hasNext();) {
Object[] obj = i.next();
System.out.println("codePerson:" +obj[0]);
System.out.println("identif:" +obj[1]);
}
addition : if you want use a mixtrue of entities and columns
@SqlResultSetMapping(name="PMapping",
entities={
@EntityResult(entityClass=Project.class,
fields={
@FieldResult(name="name",column="p_name"),
@FieldResult(name="description",column="p_desc")
})}
columns={ @ColumnResult(name="user_n") }
)
> Hibernate is ignoring native sql aliases and generating "column not found" error
> --------------------------------------------------------------------------------
>
> Key: HHH-2094
> URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2094
> Project: Hibernate Core
> Issue Type: Bug
> Components: query-sql
> Affects Versions: 3.2.0.cr4
> Environment: 3.2.0.cr4, Adaptive Server Enterprise/12.5.3
> Reporter: Leonardo Penczek
> Priority: Critical
>
> 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.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list