Announcement Announcement Module
Collapse
No announcement yet.
Query. setMaxResults(1).uniqueResult() - how in spring ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Query. setMaxResults(1).uniqueResult() - how in spring ?

    I wanna achieve something like this :

    Query q = session.createQuery("............");
    ABC obj = (ABC) q.setMaxResults(1).uniqueResult();

    The problem is HibenateTemplate doesn't have any api to access Hibernate's Query apis.
    I don't want to use
    getHibernateTemplate().getSessionFactory().openSes sion().createQuer("")

    Since that will open new session instead of using existing spring's transactional session.
    Any thought :?:

    Note: I am using hibernate 2.1.8 and spring 1.1

  • #2
    Encapsulate your logic inside a HibernateCallback where you will have full access to the current session, then simply execute your callback using the execute() method in HibernateTemplate.

    Rob

    Comment


    • #3
      Thanks. How about this code :

      Query q = SessionFactoryUtils.getSession(this.getSessionFact ory(), false).createQuery( "....");
      ABC object = (ABC) q.setMaxResults(1).uniqueResult();

      My understanding is that it should access spring's transactional session. Correct me if i am wrong.
      But this code is failing in my junit complaining :

      java.lang.IllegalStateException: No Hibernate session bound to thread, and configuration does not allow creation of new one here
      at org.springframework.orm.hibernate.SessionFactoryUt ils.getSession(SessionFactoryUtils.java:320)
      at org.springframework.orm.hibernate.SessionFactoryUt ils.getSession(SessionFactoryUtils.java:182)
      at
      I didn't quite understand why it is not recognizing the session ?

      Comment


      • #4
        I tried using callback ..it works great..thanks rob

        Comment


        • #5
          Query q = SessionFactoryUtils.getSession(this.getSessionFact ory(), false).createQuery( "....");
          change false to true and it will work this way also. However, it's preferable to use HibernateCallback.

          Comment

          Working...
          X