On Wednesday, 19 September 2012 22:39:58 UTC+1, JK wrote:

> I am semi seriously porting a project from Hibernate to jOOQ. I would like 
> to hear your ideas / comments how to port following code to jOOQ. In 
> Hibernate it is easy to build criteria in steps. Hibernate internally adds 
> "and" between each criteria components.
>

I ported a large project from Hibernate to JOOQ and couldn't be happier - 
the control that you win back is great.

In order to factor out conditions in our statements, we've used this 
construct in a few instances (excuse the Scala code, but the code should be 
isomorphic for Java):

def insertOrUpdate[R <: Record](t:Factory, table:Table[R], finders:Map[_ <: 
Field[_], _], values:Map[_ <: Field[_], _]) {
  t.insertInto(table).
      set(finders).
      set(values).
    onDuplicateKeyUpdate().
      set(values).
    execute()
} 

Basically we're passing in a map of PK fields to identify the row and a map 
of the rest of the non-PK values we want to insert into the row.

This may not be ideal solution to use everywhere, since it de-SQLizes your 
code to an extent, but it does avoid some repetition and allows to pass in 
a dynamic bag of attributes and still retain type safety(*).

FWIW another nice way I've seen this done is the way myBatis uses thread 
locals to avoid the dot notation when constructing SQL programmtically. 
However this is just syntactical sugar as it doesn't actually type check 
(*) your tables or their columns in the way JOOQ does - not to mention all 
of the other benefits JOOQ gives you.

HTH,

Ben

(*) for some value of type safety

Reply via email to