Friday, August 25, 2006

Hibernate 2.0 -> Hibernate 3.0

I spent the last two weeks (on and off) on migrating my flagship application e-nnovate e-notebook (www.e-nnovate.com) from Hibernate 2 to Hibernate 3 so I can finally support the Derby Database. There is a migration guide at the hibernate site but here are some additional tips from me:

  1. Unless you are using JTA don't use their new
    currentSession = HibernateUtil.getSessionFactory().getCurrentSession();
    because this only works with JTA. Instead stick to the ThreadLocal implementation. (In my case I always got weired errors like the Connection wasn't initialized or so)
  2. You have to replace all session.find(...) with session.generateQuery(...).list() in your code
  3. No database thing is allowed outside a transaction -- so you will have to open a transaction everywhere and in case you are doing a web application keep it open until the JSP is rendered. See here for an example which shows the principle but doesn't work. I wasn't able to open the transcation in the filter so I opened it in my code and kept it open and just used the filter to close it if it was open. In any case make sure that the filter is only running when actual jsp with database stuff are rendered.
  4. Oh, and in some cases Hibernate 3 doesn't like it if you begin another transaction inside a transaction.
  5. They got rid of dbcp instead use the cpo or proxool cache.
  6. Big bummer: they screwed up polymorphic queries with some proxy stuff. To avoid working with proxises add to the class declaration in the mapping file lazy="false". This will switch of the CGLIB optimization for that class and its subclasses.
  7. Some code like
    class bla {
    Test test = new Test();
    ....
    }
    doesn't work anymore. Instead you can't initialize Test anymore. Hibernate 3 is pretty sneeky about this and doesn't give you an error message. It just won't work.
  8. Don't forget to update XDoclet in case you are using it;-)

0 Comments:

Post a Comment

<< Home