JPAのCriteriaか(--;) vol.2


id:hayasshさんの「2009-10-28 - ひたすらプログラミング日記」を読んで、同等の事をEzJDBCで行った場合と比較してみました。。。


以下のSQLJPA 2.0で実装した例を見てみましょう。
今回は結合も無い単純なSQLですが、pointは抽出条件の「where A and(B or C) and D」の箇所でしょうか。

SELECT
  c
FROM
  Customer c
WHERE
  c.customerId BETWEEN 1 AND 10000 AND
  (c.city = 'Dearborn' OR c.city LIKE '%New%' ) AND
  c.addressline1 IS NOT NULL

JPAでは始めの面倒な設定用ソースを省略してもこんな感じです。

cq.select(r).where(qb.between(r.get(Customer_.customerId), 1,10000),
             qb.or(qb.equal(r.get(Customer_.city), "Dearborn"),
             qb.like(r.get(Customer_.city), "%New%")),
             qb.isNotNull(r.get(Customer_.addressline1)));

EzJDBCでは省略しなくてもこの程度です。

CUSTOMER c = new CUSTOMER();
EzJDBC.from(c).where( c.customerId.between(1, 10000) )
            .and( c.city.eq("Dearborn").or( c.city.like("%New%") ))
            .and( c.addressline1.isNotNull() );

JPAcqの設定ソースを省略しているとはいえ、ソースの行数的には差がありませんね。。。

ただし抽出条件の「where A and(B or C) and D 」の可読性に大きな違いがあると思います。
もちろんEzJDBCの方が可読性が高いと思っているのです…(^^;)

しかし、たかが「where A and(B or C) and D 」程度でこの可動性だったら「where A and(B and C or D and E) 」ぐらいでもやばい気がする。。。