-->
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.  [ 8 posts ] 
Author Message
 Post subject: Native SQL Bug?
PostPosted: Tue Jun 01, 2004 2:42 am 
Newbie

Joined: Sat Oct 11, 2003 1:47 pm
Posts: 17
Hibernate 2.1 with Spring Framework on jboss 3.2.3 with mysql 4.0.15

Im trying to generate a Native SQL something like
getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(net.sf.hibernate.Session session) throws net.sf.hibernate.HibernateException, SQLException
{
String sql = "select {project}.PROJECT_ID from PROJECT {project}, " +
" PROJECT_PROJECTGROUP {grp}" +
" where {project}.PROJECT_ID = {grp}.PROJECT_GROUP_ID" +
" and {project}.PROJECT_ID = " + id;
String[] aliases = {"project","grp"};
Class[] clazzes = new Class[] {Project.class, Project.class};
Query sqlQuery = session.createSQLQuery(sql, aliases, clazzes);
return sqlQuery.list();
}

}

This returns a SQL Exception:
org.springframework.jdbc.BadSqlGrammarException: Bad SQL grammar [null] in task 'HibernateAccessor'; nested exception is java.sql.SQLException: Column 'PROJECT_ID0_' not found.
java.sql.SQLException: Column 'PROJECT_ID0_' not found.
at com.mysql.jdbc.ResultSet.findColumn(Unknown Source)
at com.mysql.jdbc.ResultSet.getInt(Unknown Source)
at net.sf.hibernate.type.IntegerType.get(IntegerType.java:18)
at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
at net.sf.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:347)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:198)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:128)
at net.sf.hibernate.loader.Loader.list(Loader.java:918)
at net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:71)
at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3628)
at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:50)
at net.collab.dao.ProjectServicesHibernate$1.doInHibernate(ProjectServicesHibernate.java:104)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:150)
at net.collab.dao.Pr

There is no suffix generated in this case as im explictly called {project}.PROJECT_ID in my native SQL but still its looking for a PROJECT_ID0_ alias name which doesnt exist. Is this a bug? Any workarounds?

Thanks
Raja


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 01, 2004 3:03 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
The problem is that you have two references to the same alias/class.

This bug is fixed in v21branch of the cvs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 02, 2004 5:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
2.1.4 solve that

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 03, 2004 11:35 am 
Newbie

Joined: Thu Jun 03, 2004 10:19 am
Posts: 3
This is my code:

Code:
String sql = "SELECT {m}.* FROM MSG_OUT {m}";
List msgout = session.createSQLQuery(sql, "m", MsgOut.class).list();

I am using postgresql 7.3.6 and hibernate 2.1.4 (and the nightly tar on June 03) and it appears I am getting this same problem. I have successfully changed my query to HQL using session.find() to get it to work, but if I use native SQL it gives me the following error. Same problem if I used named queries (I'm assuming it uses the same calls). Please help.


Code:
DEBUG 11:35:42 net.sf.hibernate.SQL [main] SELECT m.* FROM MSG_OUT m
DEBUG 11:35:42 net.sf.hibernate.impl.BatcherImpl [main] preparing statement
DEBUG 11:35:58 net.sf.hibernate.loader.Loader [main] processing result set
DEBUG 11:35:58 net.sf.hibernate.util.JDBCExceptionReporter [main] SQL Exception
The column name msg_id0_ not found.
        at org.postgresql.jdbc1.AbstractJdbc1ResultSet.findColumn(AbstractJdbc1ResultSet.java:528)
        at org.postgresql.jdbc1.AbstractJdbc1ResultSet.getInt(AbstractJdbc1ResultSet.java:358)
        at net.sf.hibernate.type.IntegerType.get(IntegerType.java:18)
        at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:62)
        at net.sf.hibernate.type.NullableType.nullSafeGet(NullableType.java:53)
        at net.sf.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:352)
        at net.sf.hibernate.loader.Loader.doQuery(Loader.java:203)
        at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
        at net.sf.hibernate.loader.Loader.doList(Loader.java:955)
        at net.sf.hibernate.loader.Loader.list(Loader.java:946)
        at net.sf.hibernate.loader.SQLLoader.list(SQLLoader.java:92)
        at net.sf.hibernate.impl.SessionImpl.findBySQL(SessionImpl.java:3802)
        at net.sf.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:52)
        at hibernate.Tester.<init>(Unknown Source)
        at hibernate.Tester.main(Unknown Source)
WARN 11:35:58 net.sf.hibernate.util.JDBCExceptionReporter [main] SQL Error: 0, SQLState: null
ERROR 11:35:58 net.sf.hibernate.util.JDBCExceptionReporter [main] The column name msg_id0_ not found.
DEBUG 11:35:58 net.sf.hibernate.impl.BatcherImpl [main] done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG 11:35:58 net.sf.hibernate.impl.BatcherImpl [main] closing statement


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 03, 2004 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
that is two very different queries!

The second is is a simple .* query which is junit-tested to death, so we at least need the mapping file and the code that shows the bug...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 03, 2004 9:52 pm 
Newbie

Joined: Thu Jun 03, 2004 10:19 am
Posts: 3
Should I start a new thread and post the mapping file there ? I'm new to this forum so not too sure if I was going to get into trouble creating a new thread for a similar looking problem. I'm new to hibernate so I didn't know it was a totally different problem. Thanks for the pointers though


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 1:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
up to you ;)

just post here as the subject matches your problem - the cause of the error is just not the same.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 09, 2004 5:36 am 
Newbie

Joined: Sat Oct 11, 2003 1:47 pm
Posts: 17
From Section 13.2 of the Hibernate Documentation,
<quote>
Note: if you list each property explicitly, you must include all properties of the class and its subclasses
</quote>
So if my PROJECT Table is like
PROJECT
===========
PROJECT_ID
PROJECT_NAME
DESCRIPTION

and a native SQL Like

select proj.PROJECT_ID as {proj.projectID}," +
" proj.PROJECT_NAME as {proj.projectName} " +
" from PROJECT {proj} "

it throws me an error saying DESCRIPTION_3_0 is not found as i havent *explicitly* included that in my native SQL. Any ideas of implementing such a behaviour? I know this is overkill for native SQL and is easily represented by the OR mapping but we are trying it out for a complex SQL Query and wanted to see if we can extract specific fields of a table when doing native SQL.

Any pointers greatly appreciated for implementing such a feature

Thanks
Raja


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