-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with getHibernateTemplate().iterate
PostPosted: Fri May 29, 2009 3:02 am 
Newbie

Joined: Fri May 29, 2009 2:21 am
Posts: 12
In my application, I had a case where I needed to count no. of users from DB. So for doing this in my DAO Implementation I used the below code at first:

1.
Code:
public long getUserCount() {
            return (Long) getHibernateTemplate().iterate("select count(*) from User").next();
        }
    }


Now this throws the below exception:

------------------------------------------------------------------------------------------------------------------------------
org.hibernate.exception.GenericJDBCException: could not get next iterator result
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.handledNonSpecificException (SQLStateConverter.java: 103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.SQLStateConverter.convert (SQLStateConverter.java: 91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.exception.JDBCExceptionHelper.convert (JDBCExceptionHelper.java: 43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29) at org.hibernate.exception.JDBCExceptionHelper.convert (JDBCExceptionHelper.java: 29)
at org.hibernate.impl.IteratorImpl.next(IteratorImpl.java:125) at org.hibernate.impl.IteratorImpl.next (IteratorImpl.java: 125)
at bo.client.dao.ClientDAOHibernate.getListeParNom(ClientDAOHibernate.java:70) at bo.client.dao.ClientDAOHibernate.getListeParNom (ClientDAOHibernate.java: 70)
at tools.App.main(App.java:25) at tools.App.main (App.java: 25)
Caused by: java.sql.SQLException: Operation not allowed after ResultSet closed Caused by: java.sql.SQLException: Operation not allowed after ResultSet closed

------------------------------------------------------------------------------------------------------------------------------

Since this was not working, I used another way to implement my problem

2. Spring DataAccessUtils
Code:
public long getUserCount() throws DataAccessException {
            return DataAccessUtils.longResult(getHibernateTemplate().find("select count(*) from User"));
         }

This works smoothly.

3. Hibernate Callback method

If I implement solution for my problem using HibernateCallBack this too works smoothly, see below code

Code:
public long getUserCount(){
                      long count;
                      count = (Long) getHibernateTemplate().execute (new HibernateCallback(){
                    public Object doInHibernate(Session session) throws HibernateException,SQLException {
                        Query query = session.createQuery("select count(*) from User");
                       return ( (Long) query.iterate().next() ).longValue();
                   }
                });
            }


Hence, the only doubt that remains for me is why method (1) --> getHibernateTemplate.iterate
throws me an exception. Since Im using spring's getHibernateTemplate, I assume that all works related to
Hibernate Session, resultset would be taken care by this template, then why am I getting this weird exception that "Resultset is already closed" for method (1) and not for method (2) & (3).

Thnx
Nitesh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.