Results 1 to 8 of 8

Thread: Still need HibernateCallback when using OpenSessionInView ?

  1. #1
    Join Date
    Jul 2005
    Posts
    5

    Default Still need HibernateCallback when using OpenSessionInView ?

    Hello,


    I'm using Spring with Struts and Hibernate. I have setup the openSessionInView filter to user lazy loading.

    I'm asking if I should use HibernateCallback in my DAO methods.

    For example :
    Code:
    Integer count = (Integer) getHibernateTemplate().execute
    (
    	new HibernateCallback()
    	{
    		public Object doInHibernate( Session session ) throws HibernateException
    		{
    			Criteria criteria = session.createCriteria( Product.class );
    			criteria.add( Restrictions.like("name", productName) );
    			criteria.setProjection( Projections.rowCount() );
    					
    			return criteria.uniqueResult();
    		}
    	}
    );
    Without using a callback, the code is cleaner :

    Code:
    Criteria criteria = getSession().createCriteria( Product.class );
    criteria.add( Restrictions.like("name", productName) );
    criteria.setProjection( Projections.rowCount() );
    		
    Integer count = (Integer) criteria.uniqueResult();
    (getSession() comes from the inherited HibernateDaoSupport)


    In fact, with CallBack, I'm just writing more code and this doesn't seem of any use with the OpenSessionInView filter.

    So should I write wallback code or just use getSession() ?


    Thanks,

    Thomas

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    The advantage of using callBack is that it does some extra transaction stuff.

    Docs for getSession recommends using callback, so go with the callback

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Without using a callback, the code is cleaner :
    Indeed is shorted but what do you do in case an exception is raised? Now that HB 3 has RuntimeExceptions you are not force to catch them but it would be a good idea to do that at some point - you would have to close the session, rollback the transaction and so on.
    Even if you are using declarative demarcation (so the exceptions can change be acknowledge by the interceptor) it's way better to let spring handle everything directly (using a hibernate Template). Also the exception will be converted from HB to Spring so you'll have only one hierarchy too worry about.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  4. #4
    Join Date
    Jul 2005
    Posts
    5

    Default

    Ok, it seems clear for me now


    Go for Callback ! Yeepaaa !

    Tom

  5. #5
    Join Date
    Jun 2007
    Posts
    4

    Default HibernateCallback

    what is the major benifit to use HinernateCallback from the search

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    1) You don't have to do session management by yourself
    2) Exceptions are translated for you
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Jun 2007
    Posts
    4

    Default ViewInSession / reattach stale object

    what are the pros and cons to use viewInSession pattern? do we need worry about the reattach stale object anymore if the viewInSession got enabled? thanks?

    also can anyone give some comments on the session management in practice?

  8. #8
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    OSIV is pretty well documented, there are lots of good explanations.
    http://www.hibernate.org/43.html
    Last edited by karldmoore; Aug 29th, 2007 at 12:07 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Similar Threads

  1. Replies: 1
    Last Post: Sep 14th, 2005, 10:56 AM
  2. Alternative OpenSessionInView Semantics
    By johndstein in forum Data
    Replies: 2
    Last Post: Jul 20th, 2005, 06:09 PM
  3. Replies: 4
    Last Post: May 11th, 2005, 02:55 AM
  4. Problem with HibernateCallback
    By meissa in forum Data
    Replies: 1
    Last Post: Mar 16th, 2005, 09:50 AM
  5. Replies: 2
    Last Post: Nov 1st, 2004, 01:43 PM

Posting Permissions

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