Results 1 to 9 of 9

Thread: Use getSession() or getHibernateTemplate.getSession()?

  1. #1
    Join Date
    Oct 2005
    Posts
    17

    Default Use getSession() or getHibernateTemplate.getSession()?

    I'm unsure of which of the above to use in our DAOs (which extend HibernateDAOSupport).

    The SpringFramework API doc on HibernateTemplate.getSession() is thus:

    protected Session getSession()

    Return a Session for use by this template.

    Returns a new Session in case of alwaysUseNewSession" (using the same JDBC Connection as a transactional Session, if applicable), a pre-bound Session in case of "allowCreate" turned off, and a pre-bound or new Session else (new only if no transactional or otherwise pre-bound Session exists).


    The SpringFramework API doc on getSession() is thus:

    protected final Session getSession()
    throws DataAccessResourceFailureException,
    IllegalStateException

    Get a Hibernate Session, either from the current transaction or a new one. The latter is only allowed if the "allowCreate" setting of this bean's HibernateTemplate is true.

    Note that this is not meant to be invoked from HibernateTemplate code but rather just in plain Hibernate code. Either rely on a thread-bound Session (via HibernateInterceptor), or use it in combination with releaseSession.

    In general, it is recommended to use HibernateTemplate, either with the provided convenience operations or with a custom HibernateCallback that provides you with a Session to work on. HibernateTemplate will care for all resource management and for proper exception conversion.


    So it would seem that best practice would be to use getHibernateTemplate().getSession, instead of getSession()?

    Thanks,

    s.l.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    I'd recommend using HibernateTemplate.getSession(). However calling getSession() method is not normally recommended, as the following from the Javadoc you quote states:
    In general, it is recommended to use HibernateTemplate, either with the provided convenience operations or with a custom HibernateCallback that provides you with a Session to work on. HibernateTemplate will care for all resource management and for proper exception conversion.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  3. #3
    Join Date
    Oct 2005
    Posts
    17

    Default

    Thanks for the clear and timely response.

    s.l.

  4. #4
    Join Date
    Oct 2005
    Posts
    17

    Default

    Hmmm. I'm re-reading your reply and am perhaps a bit confused. You state:

    I'd recommend using HibernateTemplate.getSession(). However calling getSession() method is not normally recommended...
    Not sure if the above means:

    a) use getHibernateTemplate().getSession() instead of getSession() alone

    or

    b) It's not recommended to call getSession() anytime, even the HibernateTemplate.getSession() - use other aspects of the HibernateTemplate to do session stuff.

    Sorry if I didn't follow - can you clarify?

    Regards,

    s.l.

  5. #5
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    b)

  6. #6
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    I meant (b), for the reasons stated in the Javadoc excerpt I quoted.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  7. #7
    Join Date
    Oct 2005
    Posts
    17

    Default

    Unfortunately, we make heavy use of sessions, to create queries like thus:

    Code:
    	String hql = "FROM ExecutionSummary WHERE datamartSeq=:seq order by endTime desc";
    	List list = getSession().createQuery(hql)
    							.setLong("seq",datamartSeq.longValue())
    							.setMaxResults(count)
    							.list();
    and so:

    Code:
        	Integer count = (Integer) getSession().
        		createQuery("select count(*) FROM FolderContent fc where fc.folder=:folder").
        		setEntity("folder", folder).uniqueResult();
    I could have missed it, but I don't see a createQuery() method for HibernateTemplate:

    http://<br /> <a href="http://www.s...html</a><br />

    Is there another way to create queries? There are methods available via queries that have no equivalent in HibernateTemplate (like uniqueResult()).

    Also (and I don't expect an answer to this, but maybe a comment), "Hibernate in Action", chapter 7, is chock-a-block full of using session to create queries. If the API says "don't do this", I can't understand why the authors of this book would use it anyway.

    s.l.

  8. #8
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    For direct access to the session you can use HibernateCallback with HibernateTemplate.
    Rod Johnson - GM, SpringSource Division, VMware
    http://www.springsource.com
    Spring From the Source

  9. #9
    Join Date
    Oct 2005
    Posts
    17

    Default

    Ok. So in the Spring online doc:

    http://www.springframework.org/docs/reference/orm.html

    I found section 12.2.3 - Inversion of Control: HibernateTemplate and HibernateCallback, which has code samples on how to do session stuff using HibernateCallback.

    However, the same document also has section 12.2.4 - Implementing Spring-based DAOs without callbacks. This section's code sample uses getSession(), which is what you recommend against.

    Getting some mixed messages here...

Similar Threads

  1. Replies: 5
    Last Post: Jul 13th, 2005, 01:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •