Announcement Announcement Module
Collapse
No announcement yet.
simple, stupid hibernate3/spring questions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • simple, stupid hibernate3/spring questions

    I've done the examples, I have no problem getting hibernate3 and spring to play nice together. Now, I'm working on some real life stuff, and I'm starting to have questions on how I want to build stuff.

    So far, I've seen 3 ways to implement my BaseDao.

    1.) BaseDaoImpl extends HibernateDaoSupport

    Using this, I'd get my HibernateTemplate via the getHibernateTemplate method.

    2.) BaseDaoImpl does not extend anything

    Using this, I'd get my HibernateTemplate by:
    HibernateTemplate ht = new HibernateTemplate(sessionFactory);

    3) Same as #2, only you use DI to inject the HibernateTemplate

    What are the advantages/disavantages/reasons for the various ways above.

    One other question, when persisting an object, do I want to use HibernateTemplate.save() or HibernateTemplate.persist() . The javadocs don't really give a difference between the two.

  • #2
    In any framework/language there is more then one way to do things. The difference are in some cases subtle and depend on the preferences of the author.
    In general is good to favor composition instead of inheritance - if you are using the functionality from HibernateDaoSupport (or you will need it) it makes sense to extend it.
    Between 2 and 3 I would go with 3 since as HibernateTemplate is per-session factory you can reuse the instance without problems between multiple DAOs.
    This will not necessary yield better performance (or lower memory usage since HibernateTemplate is a lightweight class) but resemble better with a stateless approach. Again, in the end all 3) approaches are valid so it's up to you.

    Comment


    • #3
      Thanks! I was leaning towards #2 or #3, but just wanted to make sure I wasn't overlooking anything by not using HibernateDaoSupport.

      Is it basically the same answer for the HibernateTemplate.save() and HibernateTemplate.persist() methods? Same result, just different name of method?

      Comment

      Working...
      X