-->
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.  [ 2 posts ] 
Author Message
 Post subject: AbstractQueryImpl.getSession (package visibilty???)
PostPosted: Sun Feb 15, 2004 7:47 am 
Newbie

Joined: Fri Jan 23, 2004 4:02 am
Posts: 6
Location: Linz, Austria
Is there a good reason that getSession() is not a public method (like getQueryString). I know data hiding/encapsulation is of importance and should be enforced where possible!
The query string has a public getQueryString and I was just wondering if mistakenly the public/protected modifier for getSession was forgotten?

I'm writing adapters for various data sources for a datagrid JSP component. The data sources may be simple collections, lists etc. I also support hibernate queries. For supporting paging I have defined an interface to request an iterator (for the elements of a page) and the size of the collection(for computing the number of pages).
For a hibernate query I need to to modify the original query string such that I can retrieve just a "count(*) for some query. With this modified query I have to create a new query instance and therefore I need access to the session field.

I can't get the session from anywhere else since I can't make assumptions who has created the passed query instance.
Meanwhile I have helped by using reflection but with some SecurityManager that might fail!

Code:

public QueryDataSourceImpl(Query query) {
   super();
   this.query = query;
}

public Iterator iterator(int fromIndex, int toIndex) throws DataSourceException {
   query.setFirstResult(fromIndex);
   query.setMaxResults(toIndex - fromIndex);
   try {
      return query.list().iterator();
   } catch (HibernateException e) {
      throw new DataSourceException(e);
   }
}

public int size() throws DataSourceException {
   String queryString = query.getQueryString();
   int fromPosition = queryString.toLowerCase().indexOf("from ");
   if (fromPosition >= 0) {
      queryString = "select count(*) " + queryString.substring(fromPosition);
   }
   Method getSessionMethod;
   try {
      getSessionMethod = query.getClass().getSuperclass().getDeclaredMethod("getSession", new Class[] {
      });
      getSessionMethod.setAccessible(true);
      Session session = (Session) getSessionMethod.invoke(query, new Object[] {
      });
      Query sizeQuery = session.createQuery(queryString);
      return sizeQuery.list().size();
   } catch (SecurityException e1) {
   } catch (NoSuchMethodException e1) {
   } catch (HibernateException e) {
      throw new DataSourceException(e);
   } catch (IllegalArgumentException e) {
   } catch (IllegalAccessException e) {
   } catch (InvocationTargetException e) {
   }
   try {
      return query.list().size();
   } catch (HibernateException e) {
      throw new DataSourceException(e);
   }
}



Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 4:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
It has not been forgotten....have not had any reason for exposing it....don't think it should be needed in normal cases.

Put it on the JIRA and we will see ;)

_________________
Max
Don't forget to rate


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