Announcement Announcement Module
Collapse
No announcement yet.
Still need HibernateCallback when using OpenSessionInView ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    The advantage of using callBack is that it does some extra transaction stuff.

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

    Comment


    • #3
      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.

      Comment


      • #4
        Ok, it seems clear for me now


        Go for Callback ! Yeepaaa !

        Tom

        Comment


        • #5
          HibernateCallback

          what is the major benifit to use HinernateCallback from the search

          Comment


          • #6
            1) You don't have to do session management by yourself
            2) Exceptions are translated for you

            Comment


            • #7
              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?

              Comment


              • #8
                OSIV is pretty well documented, there are lots of good explanations.
                http://www.hibernate.org/43.html
                Last edited by karldmoore; Aug 29th, 2007, 01:07 PM.

                Comment

                Working...
                X