Announcement Announcement Module
Collapse
No announcement yet.
How to obtain a net.sf.hibernate.Session object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to obtain a net.sf.hibernate.Session object

    Hi!

    I'm using Spring 1.1.2.

    Is there a way to obtain a current Session object which is used by HibernateTemplate? Can I in some way create a Criteria object?

    In the Spring 1.1.5 API doc I can see getSession() but it's protected.

    In 1.2.2 there seems to be no such method at all.

    Will there be some minor/major changes if I move on to the latest stable version of Spring? I'd rather not do any major changes to my web app at this moment.

  • #2
    You can have a class implement HibernateCallback, and then call HibernateTemplate.execute() on an instance of that class. The HibernateTemplate will call doInHibernate and pass in the Session object.

    Comment


    • #3
      Something like that

      getHibernateTemplate().execute(new HibernateCallback()
      {
      public Object doInHibernate(Session session) throws HibernateException {
      return session.createCriteria
      .add( Expression.eq("swiftCodeName", code) )
      .setCacheable(true)
      .uniqueResult();
      }
      });

      Comment


      • #4
        Thanks, both of you. I'll try that today.

        Comment


        • #5
          Follow-up:

          Can I somehow combine session.find() with a criteria?

          Comment

          Working...
          X