Announcement Announcement Module
Collapse
No announcement yet.
Difference between HibernateCallback and plain hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    refresh

    Hi,

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

    Thanks in advance for your time.

    chris

    Comment


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

      Comment


      • #4
        thanks, that was what I thought.

        Comment

        Working...
        X