-->
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: How to project specific columns using Native SQL
PostPosted: Fri Jul 13, 2007 1:47 am 
Newbie

Joined: Fri Jul 13, 2007 1:20 am
Posts: 2
Location: Makati City, Philippines
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
version 3
Mapping documents:
xdoclet
Code between sessionFactory.openSession() and session.close():

Query query =sess.createSQLQuery("SELECT msg.id, msg.message, msg.receiverUserId FROM Messages msg, Users u WHERE receiverUserId = :receiverUserId").addEntity(Message.class).addEntity(User.class);
query.setString("receiverUserId", _userId.toString());
log.debug("*********the query********"+query.getQueryString());
for (Iterator i = query.list().iterator(); i.hasNext(); ) {
log.debug("****"+(Message)i.next());

}

Full stack trace of any exception that occurs:

2007-07-13 13:28:21,509 (org.hibernate.type.NullableType:132) 87004 INFO - could not read column value from result set: dateSent; The column name dateSent was not found in this ResultSet.
2007-07-13 13:28:21,537 (org.hibernate.util.JDBCExceptionReporter:71) 87032 WARN - SQL Error: 0, SQLState: 42703
2007-07-13 13:28:21,538 (org.hibernate.util.JDBCExceptionReporter:72) 87033 ERROR - The column name dateSent was not found in this ResultSet.
[13:28:21.579] org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
[13:28:21.579] Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
[13:28:21.579] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
[13:28:21.579] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
[13:28:21.579] at org.hibernate.loader.Loader.doList(Loader.java:2148)
[13:28:21.579] at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
[13:28:21.579] at org.hibernate.loader.Loader.list(Loader.java:2024)
[13:28:21.579] at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
[13:28:21.579] at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1655)
[13:28:21.579] at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
[13:28:21.579] at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
[13:28:21.579] at net.hoparound.repository.messages.hibernate.MessageDAOImpl.getAllMessagesReceivedByUserId(MessageDAOImpl.java:49)
[13:28:21.579] at net.hoparound.manager.messages.MessageManagerImpl.getAllMessagesReceivedByUserId(MessageManagerImpl.java:40)
[13:28:21.579] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[13:28:21.579] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[13:28:21.579] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[13:28:21.579] at java.lang.reflect.Method.invoke(Method.java:585)
[13:28:21.579] at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
[13:28:21.579] at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:198)
[13:28:21.579] at $Proxy17.getAllMessagesReceivedByUserId(Unknown Source)
[13:28:21.579] at net.hoparound.facade.messages.MessageFacade.getAllMessagesReceivedByUserId(MessageFacade.java:59)
[13:28:21.579] at net.hoparound.web.message.InboxController.referenceData(InboxController.java:62)
[13:28:21.579] at org.springframework.web.servlet.mvc.AbstractFormController.showForm(AbstractFormController.java:557)
[13:28:21.579] at org.springframework.web.servlet.mvc.SimpleFormController.showForm(SimpleFormController.java:193)
[13:28:21.579] at org.springframework.web.servlet.mvc.SimpleFormController.showForm(SimpleFormController.java:172)
[13:28:21.579] at org.springframework.web.servlet.mvc.AbstractFormController.showNewForm(AbstractFormController.java:323)
[13:28:21.579] at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:263)
[13:28:21.579] at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
[13:28:21.579] at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
[13:28:21.579] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:806)
[13:28:21.579] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:736)
[13:28:21.579] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
[13:28:21.579] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:350)
[13:28:21.579] at javax.servlet.http.HttpServlet.service(HttpServlet.java:115)
[13:28:21.579] at javax.servlet.http.HttpServlet.service(HttpServlet.java:92)
[13:28:21.579] at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:103)
[13:28:21.579] at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:264)
[13:28:21.579] at org.acegisecurity.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:107)
[13:28:21.579] at org.acegisecurity.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:72)
[13:28:21.579] at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
[13:28:21.579] at org.acegisecurity.ui.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:110)
[13:28:21.579] at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
[13:28:21.579] at org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:81)
[13:28:21.579] at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
[13:28:21.579] at org.acegisecurity.ui.AbstractProcessingFilter.doFilter(AbstractProcessingFilter.java:217)
[13:28:21.579] at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
[13:28:21.579] at org.acegisecurity.context.HttpSessionContextIntegrationFilter.doFilter(HttpSessionContextIntegrationFilter.java:193)
[13:28:21.579] at org.acegisecurity.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:274)
[13:28:21.579] at org.acegisecurity.util.FilterChainProxy.doFilter(FilterChainProxy.java:148)
[13:28:21.579] at org.acegisecurity.util.FilterToBeanProxy.doFilter(FilterToBeanProxy.java:98)
[13:28:21.579] at com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:73)
[13:28:21.579] at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:167)
[13:28:21.579] at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:226)
[13:28:21.579] at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:263)
[13:28:21.579] at com.caucho.server.port.TcpConnection.run(TcpConnection.java:477)
[13:28:21.579] at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:591)
[13:28:21.579] at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:513)
[13:28:21.579] at java.lang.Thread.run(Thread.java:595)
[13:28:21.579] Caused by: org.postgresql.util.PSQLException: The column name dateSent was not found in this ResultSet.
[13:28:21.579] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.findColumn(AbstractJdbc2ResultSet.java:2365)
[13:28:21.579] at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getTimestamp(AbstractJdbc2ResultSet.java:2248)
[13:28:21.579] at org.hibernate.type.TimestampType.get(TimestampType.java:30)
[13:28:21.579] at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:113)
[13:28:21.579] at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:102)
[13:28:21.579] at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
[13:28:21.579] at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:1899)
[13:28:21.579] at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1372)
[13:28:21.579] at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1300)
[13:28:21.579] at org.hibernate.loader.Loader.getRow(Loader.java:1197)
[13:28:21.579] at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
[13:28:21.579] at org.hibernate.loader.Loader.doQuery(Loader.java:689)
[13:28:21.579] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
[13:28:21.579] at org.hibernate.loader.Loader.doList(Loader.java:2145)
[13:28:21.579] ... 53 more
[13:28:22.203] Closing dangling connections. All connections must have a close() in a finally block.
[13:28:22.204] java.lang.IllegalStateException: Connection UserConnection[com.caucho.sql.ManagedConnectionImpl@6e1f8f] was not closed. Connections must have a close() in a finally block.
[13:28:22.204] at com.caucho.jca.UserTransactionImpl.abortTransaction(UserTransactionImpl.java:490)
[13:28:22.204] at com.caucho.jca.UserTransactionProxy.abortTransaction(UserTransactionProxy.java:182)
[13:28:22.204] at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:187)
[13:28:22.204] at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:226)
[13:28:22.204] at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:263)
[13:28:22.204] at com.caucho.server.port.TcpConnection.run(TcpConnection.java:477)
[13:28:22.204] at com.caucho.util.ThreadPool$Item.runTasks(ThreadPool.java:591)
[13:28:22.204] at com.caucho.util.ThreadPool$Item.run(ThreadPool.java:513)
[13:28:22.204] at java.lang.Thread.run(Thread.java:595)


Name and version of the database you are using:
PostgreSQL 8.1.8
The generated SQL (show_sql=true):

SELECT msg.id, msg.message, msg.receiverUserId FROM Messages msg, Users u WHERE receiverUserId = :receiverUserId

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

I have a Messages table on which i would only like to query specific fields.
Whenever I use addScalar to filter the projection the result will just be plain array of Objects not an Object of my domain which is 'Message'. So I tried extracting the columns by specifying them on my query [msg.id, msg.receiverUserId .. .] but every time i run the code it keeps on finding the next column and the error won't end unless i use * to retrieve all columns.which i dont like doing since loading time will suffer if i do that.

i also tried using the Projection interface but it returns the exact records in the database not the Object of my Message domain.
Pls help me....


Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 2:46 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

use createCriteria()

and

setResultTransformer()

I don't see anything related to Users u in your sql query.
if you send correct querry i can code it.

for given query try this.

Code:
List list=session.createCriteria(Messages.class,"msg")
             .setProjection(Projections.projectionList()
                   .add(Projections.property("msg.id"),"id")
                   .add(Projections.property("msg.message"),"message")
                   .add(Projections.property("msg.receiverUserId"),"receiverUserId")
              )
              .add(Restrictions.eq("msg.receiverUserId",_userId.toString()))
              .setResultTransformer(new AliasToBeanResultTransformer(Messages.class))
.list();



Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 3:47 am 
Newbie

Joined: Fri Jul 13, 2007 1:20 am
Posts: 2
Location: Makati City, Philippines
That's great! Thanks so much!
By the way I just experimented with my query that's why User was there.
I also experimented with setCriteria and Projection but i use the wrong transformer (.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)) :(
AliasToBeanResultTransformer solves it.

Thanks! alot for helping me out :)


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.