Results 1 to 4 of 4

Thread: Difference between HibernateCallback and plain hibernate

  1. #1

    Default Difference between HibernateCallback and plain hibernate

    Hi,

    I've read the chapter 12 of the spring documentation but I still do not understand something about HibernateTemplate.execute and plain Hibernate HQL.

    here is my example :
    Code:
    //NOTE: uid is a final String
       //1st way
            this.getHibernateTemplate().execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException {
                    String hql = "DELETE FROM UserSession us WHERE us.uid = ?";
                    session.createQuery(hql).setString(0, uid).executeUpdate();
                    return null;
                }
            });
    
       //2nd way
            String hql = "DELETE FROM UserSession us WHERE us.uid = ?";
            this.getSession().createQuery(hql).setString(0, uid).executeUpdate();
    In the 1st way, I know that it will use the good transaction and the good session and also the exceptions will be converted from Hibernate hierarchy to Spring hierarchy.
    Is that it ?

    in the 2nd way, what is wrong ? Are the transactions correctly used ? Is the exception conversion performed ?

    Thanks for any explanations.
    Note : if there is some documentation clearer than chapter 12 on the subject, I will also appreciate a hint

    Regards,
    chirs

  2. #2

    Default refresh

    Hi,

    I really need an answer on this question to fully understand the HibernateTemplate mecanism.

    Thanks in advance for your time.

    chris

  3. #3
    Join Date
    Sep 2006
    Posts
    2

    Default

    I'm pretty sure the only difference is that using the callback ensures Hibernate exceptions will be converted to the Spring exception hierarchy. You can do the same by wrapping the 2nd example in a try/catch.

  4. #4

    Default

    thanks, that was what I thought.

Posting Permissions

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