Category Archives: Hibernate

Hibernate DetachedCriteria is not reusable

Once you execute following code:

Criteria criteria = detachedCriteria
.getExecutableCriteria(session);

you cannot reause the detachedCriteria for creating another one executable criteria because they are connected together (e.g. setting projection or pagination for criteria modifies also detachedCriteria object).

You must do following workarounds:

criteria.setMaxResults(0);
criteria.setFirstResult(0);

detachedCriteria.setResultTransformer( Criteria.ROOT_ENTITY );
detachedCriteria.setProjection( null );

For more info see Hibernate forum –
http://forum.hibernate.org/viewtopic.php?t=939308

2tier application using Hibernate

I got involved in new project in Janury. The goal was to develop new version of existing application. It must be 2tier RCP and it must be albe work over many databases (used by 16 banks, political reasons).

We decided to use Eclipse RCP and Hibernate.
Eclipse RCP work’s fine. SWT and JFace makes gui development quite easy.
But using Hibernate in 2tier application brings many difficulties:

  • Session slows down when contains many (hundreds) objects
  • You cannot use Session after you got some error on it
  • There are some bugs in HQL that make advanced usage quite difficult

See Hibernate in 2 tier application discussiong Is it Possible to Combine Hibernate + Swing?????.

After 5 months (I became the project leader meanwhile) we eneded up with 2,5 tier design:

  • use DAO classes to load light objects displayed in views (eg. load, initialize and close session in getChildren methods)
  • use SQL and HQL for batch updates where possible
  • use session per editor, load with LockMode.UPGRADE
  • use Maps instead of querying database
  • divide batch processing into small part, each in new session and transaction or use StateLess session

Advanced Hibernate bugs

PS: next version must be 3-tier application.