Page 1 of 3 123 LastLast
Results 1 to 10 of 22

Thread: using getSession() instead of hibernateTemplate

  1. #1
    Join Date
    Feb 2009
    Posts
    6

    Thumbs up using getSession() instead of hibernateTemplate

    Hi

    Am a beginner to Spring.

    I have an existing application in hibernate. There is a plan to implement Spring in the application and using spring-hibernate. This should happen with as much minimal changes as possible to the existing code.

    My current application structure is as follows:

    Current application has one Generic DAO which has all standard methods to retrieve and persist. And all the dao's extend this generic DAO and they will have custom methods which handles complex queries.

    We are passing session to the DAO's through constructor and using that session for executing the queries.

    1. Now If want to use Spring-Hibernate, using HibernateTemplate is the best, but I have to touch all the DAO's and each and every method in those DAO's to wrap the queries with hibernate template. Please suggest if you have some thing better, which we can be injected into the DAO's without touching the all the methods in the DAO.

    2. One solution that I have is, use getSession() of HibernateSupportDAO instead of hibernateTemplate(). And I'll be implementing transactions through Spring AOP. If so, will the session closing be handled by the transaction interceptor? Or Do I need to take care of closing the session in the code explicitly?

    Could any one please provide a suggestion?
    Thanks in Advance.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,626

    Default

    Neither. It isn't recommended to use either HibernateDaoSupport or HibernateTemplate any any more.

    Create a plain hibernate based dao and use the getCurrentSession method not openSession (important!!!!). That in combination with declarative transaction management is enough to get all the nice stuff from hibernate and from spring.

    I suggest you read the reference guide especially the hibernate chapter.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Sep 2008
    Location
    Hyderabad
    Posts
    27

    Default

    Hi Marten,
    Could you please elaborate your suggestion....
    Thanks,
    K. Siva Prasad Reddy

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,626

    Default

    As I stated, read the chapter about hibernate that explains it all. You wan to read chapter 12.2.5.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Feb 2009
    Posts
    6

    Default

    Thanks Martin.

    I read the reference guide and implemented with plain Hibernate DAO and Spring JTA. It is working.

    Thanks a lot.

  6. #6
    Join Date
    Apr 2008
    Posts
    17

    Default

    Quote Originally Posted by Marten Deinum View Post
    Neither. It isn't recommended to use either HibernateDaoSupport or HibernateTemplate any more.
    As I see there is an advantage using HibernateTemplate over plan Hibernate3 DAO: hibernate exceptions are automatically converted into DataAccessExceptions.

    Is it possible to achive this while using plain hibernate3 daos without HibernateTemplate?

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,626

    Default

    As I already suggested 2 times, read the reference guide, the link is there which explains it all.

    Quote Originally Posted by byto
    Is it possible to achive this while using plain hibernate3 daos without HibernateTemplate?
    Yes it is, else the template/daosupport would still be the way to go...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Apr 2008
    Posts
    17

    Default

    Quote Originally Posted by Marten Deinum View Post
    Yes it is, else the template/daosupport would still be the way to go...
    So please tell me how I get the hibernate exceptions wrapped while using plain hibernate 3 daos!? After deleting HibernateTemplate from my DAOs and using getCurrentSession() instead, one of my unittests throws a hibernate related exception instead of wrapping it with DataIntegrityViolationException from Spring.

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,626

    Default

    As I suggested 3 times already RTFM... Chapter 12.6.4 explains it all...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  10. #10
    Join Date
    Apr 2008
    Posts
    17

    Default

    Cant get it work. I think its because Im using HibernateTransactionManager instead of JpaTransactionManager? IIRC the HibernateJpaDialect is used to translate hibernate exceptions to data access exceptions. But I dont see how I can set up one of those while using HibernateTransactionManager.

Posting Permissions

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