Wednesday 12 August 2009

Cannot call an overloaded constructor (in a Java class) from Groovy (1.6.3)

So I have a Java class with two constructors. If I "call" one of them from Groovy, the code compiles but the constructor is never called. Nice.

Thursday 6 August 2009

Hibernate - could be a bit brighter

I love Hibernate - I think it is great. I have seen, and made some hideous applications with Hibernate that run like a dog. With three legs. Two of which are broken, but in pretty much every case, the inefficiency has been down to the use of Hibernate, rather than Hibernate itself. I have no time for people who bash Hibernate because "it is slow". It isn't. It is a tool which needs to be used properly. Almost all the projects I have worked on that have used Hibernate have also included custom JDBC. Anyway, just occasionally, Hibernate wants to make me rip my hair out in frustration. The latest example... I issue the following hql:
select session.id from Session session where session.mask is null or session.mask.id=8013
and I get back no results. Strange, I look in the DB and there are plenty of Sessions with null masks. Hibernate generates the following SQL:
  select
      session0_.id as col_0_0_
 from
      t_session session0_,
      t_mask mask2_
  where
     session0_.mask_id=mask2_.id
      and (
          session0_.mask_id is null
          or mask2_.stream_id=8013
      )
  order by
      session0_.start_date
1 prize for spotting the *stupid stupid stupid* assumption that hibernate made. The solution is to issue the following hql:
select session.id from Session session left outer join session.mask sessionMask where sessionMask is null or sessionMask.id=8013
which issues:
  select
      session0_.id as col_0_0_
  from
      t_session session0_
  left outer join
      t_mask mask1_
          on session0_.mask_id=mask1_.id,
      t_resource resource2_
  where
          mask1_.id is null
          or mask1_.stream_id=8013
  order by
      session0_.start_date
I am sure the manual warns you about this, but really. Hibernate, come on.