Can someone please help me understand why I am getting the exception below when trying to execute this query:
String sql = "select u.username, f.fname from user u, file f where f.OwnerID = u.ID";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity("u", User.class);
query.addEntity("f", AppFile.class);
AppFile.hbm.xml: <class name="beans.AppFile" table="FILE">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="fSize"/>
<property name="fName"/>
<property name="vaultID"/>
<property name="ownerID"/>
<property name="status"/>
<property name="createdOn" type="timestamp" column="createdon"/>
<property name="modifiedOn" type="timestamp" column="modifiedon"/>
<property name="modifiedBy"/>
User.hbm.xml:
<class name="beans.User" table="USER">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="userName"/>
<property name="firstName"/>
<property name="lastName"/>
<property name="emailAddress"/>
<property name="password"/>
</class>
Is it not possible to use the primary key of a table in such queries?
The ID column does exist in the database, the USer bean has getters and setters for Id which is defined as a Long.
Hibernate: select u.username, f.fname from user u, file f where f.OwnerID = u.ID
00:22:40,649 INFO LongType:182 - could not read column value from result set: I
D; Column 'ID' not found.
00:22:40,665 WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: S0022
00:22:40,665 ERROR JDBCExceptionReporter:78 - Column 'ID' not found.
00:22:40,665 ERROR [Controller]:260 - Servlet.service() for servlet Controller t
hrew exception
org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.j
ava:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelp
er.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2216)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
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 servlet.Controller.listFiles(Unknown Source)
at servlet.Controller.doPost(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.p
rocessConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFol
lowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.sql.SQLException: Column 'ID' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1093)
at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:2942)
at org.hibernate.type.LongType.get(LongType.java:28)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.loader.Loader.getKeyFromResultSet(Loader.java:1097)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:565)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Lo
ader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
... 24 more
|