0

I'm using Hibernate's getHibernateTemplate().findByNamedQuery() in order to execute a stored procedure in SQL Server (for optimization reasons). The stored proc is supposed to return a List of Longs.

From what I can see, the only way I can return such a list is to create a wrapper class specifically for Hibernate, and use this in part of my @NamedNativeQuery declaration: resultClass=LongWrapper.class

Is this really the only way to return a list of longs using Hibernate's findByNamedQuery() function?

Thing is, I need to optimize memory allocation and performance, as the stored procedure will return close to a million longs, so I'm reluctant wrap already-wrapped Longs into my own wrapper class. Seems like unnecessary overhead.

Any suggestions?

Thank you in advance!!

  • so, are you able to call a SP using **findByNamedQuery()?** There are other ways ton call a stored procedure form spring. Your question is about this method? – ManuPK Apr 19 '12 at 14:20

1 Answers1

1

You can rewrite your method to the following:

@SuppressWarnings("unchecked")
public List<Long> getIds() {
  Session session = getSession();
  Query namedQuery = session.getNamedQuery(QUERY_IDS);
  String queryForIds = namedQuery.getQueryString();
  SQLQuery query = session.createSQLQuery(queryForIds);
  query.addScalar("ID", Hibernate.LONG);
  List<Long> result = query.list();
  return result;            
}

I got this solution from how-do-i-use-hibernates-findbynamedquery-to-return-a-list-of-longs

Community
  • 1
  • 1