ホーム > タグ > S2JDBC

S2JDBC

2way SQL の挙動がおかしい(気がする)@S2JDBC

S2JDBC の 2way SQL の挙動が気になったのでメモ。
というか 2way SQL のパラメータが Map のときの挙動。

チュートリアルのテストケースを利用して検証してみました。
2.4.35 を使用します。

まずサンプルの 2way SQL をコピーして変更。

■ META-INF/sql/examples/entity/Employee/selectWithDepartment2.sql

  1.  select e.*, d.name as department_name
  2.  from employee e left outer join department d on e.department_id = d.id
  3.  /*BEGIN*/
  4.  where
  5.   /*IF salaryMin != null*/
  6.      e.salary >= /*salaryMin*/1000
  7.   /*END*/
  8.   /*IF salaryMax != null*/
  9.      and e.salary <= /*salaryMax*/2000
  10.   /*END*/
  11.   – 追加ここから
  12.   /*IF departmentId != null*/
  13.      and e.department_id = /*departmentId*/1
  14.   /*END*/
  15.   – 追加ここまで
  16.  /*END*/
  17.  order by e.salary

■ テストケースその1
パラメータをすべて指定してみる。

  1.  public void testSqlFile() throws Exception {
  2.  
  3.          // DTO
  4.          SelectWithDepartmentDto dto = new SelectWithDepartmentDto();
  5.          dto.salaryMin = 1200;
  6.          dto.salaryMax = 1800;
  7.          dto.departmentId = 3;
  8.          List<EmployeeDto> results =
  9.              jdbcManager
  10.                  .selectBySqlFile(EmployeeDto.class, SQL_FILE, dto)
  11.                  .getResultList();
  12.          System.out.println("—DTO—————————");
  13.          for (EmployeeDto e : results) {
  14.              System.out
  15.                  .println(e.name + " " + e.salary + " " + e.departmentName);
  16.          }
  17.          System.out.println("————————-DTO—–");
  18.  
  19.          // MAP
  20.          Map map = new HashMap();
  21.          map.put("salaryMin", 1200);
  22.          map.put("salaryMax", 1800);
  23.          map.put("departmentId", 3);
  24.  
  25.          List<EmployeeDto> results2 =
  26.              jdbcManager
  27.                  .selectBySqlFile(EmployeeDto.class, SQL_FILE, map)
  28.                  .getResultList();
  29.          System.out.println("—MAP—————————");
  30.          for (EmployeeDto e : results2) {
  31.              System.out
  32.                  .println(e.name + " " + e.salary + " " + e.departmentName);
  33.          }
  34.          System.out.println("————————MAP——");
  35.  
  36.      }

同じ SQL が発行されます。当然結果も同じです。

■ テストケースその2
パラメータを2つにしてみた。

  1.  public void testSqlFile2() throws Exception {
  2.  
  3.          // DTO
  4.          SelectWithDepartmentDto dto = new SelectWithDepartmentDto();
  5.          dto.salaryMin = 1200;
  6.          dto.salaryMax = 1800;
  7.          List<EmployeeDto> results =
  8.              jdbcManager
  9.                  .selectBySqlFile(EmployeeDto.class, SQL_FILE, dto)
  10.                  .getResultList();
  11.          System.out.println("—DTO—————————");
  12.          for (EmployeeDto e : results) {
  13.              System.out
  14.                  .println(e.name + " " + e.salary + " " + e.departmentName);
  15.          }
  16.          System.out.println("————————-DTO—–");
  17.  
  18.          // MAP
  19.          Map map = new HashMap();
  20.          map.put("salaryMin", 1200);
  21.          map.put("salaryMax", 1800);
  22.  
  23.          List<EmployeeDto> results2 =
  24.              jdbcManager
  25.                  .selectBySqlFile(EmployeeDto.class, SQL_FILE, map)
  26.                  .getResultList();
  27.          System.out.println("—MAP—————————");
  28.          for (EmployeeDto e : results2) {
  29.              System.out
  30.                  .println(e.name + " " + e.salary + " " + e.departmentName);
  31.          }
  32.          System.out.println("————————MAP——");
  33.  
  34.      }

同じ SQL が発行され、結果も同じなのですが、Map の方では WARN が出力される。

WARN 2009-05-04 06:52:33,250 [main] 引数(departmentId)が見つかりません

以下のように Map に key を設定すれば回避できますが…(ーー;)

  1.  map.put("departmentId", null);

■ テストケースその3
パラメータを1つにしてみた。

  1.  public void testSqlFile3() throws Exception {
  2.  
  3.          // DTO
  4.          SelectWithDepartmentDto dto = new SelectWithDepartmentDto();
  5.          dto.salaryMin = 1200;
  6.          List<EmployeeDto> results =
  7.              jdbcManager
  8.                  .selectBySqlFile(EmployeeDto.class, SQL_FILE, dto)
  9.                  .getResultList();
  10.          System.out.println("—DTO—————————");
  11.          for (EmployeeDto e : results) {
  12.              System.out
  13.                  .println(e.name + " " + e.salary + " " + e.departmentName);
  14.          }
  15.          System.out.println("————————-DTO—–");
  16.  
  17.          // MAP
  18.          Map map = new HashMap();
  19.          map.put("salaryMin", 1200);
  20.  
  21.          List<EmployeeDto> results2 =
  22.              jdbcManager
  23.                  .selectBySqlFile(EmployeeDto.class, SQL_FILE, map)
  24.                  .getResultList();
  25.          System.out.println("—MAP—————————");
  26.          for (EmployeeDto e : results2) {
  27.              System.out
  28.                  .println(e.name + " " + e.salary + " " + e.departmentName);
  29.          }
  30.          System.out.println("————————MAP——");
  31.  
  32.      }

Map のとき、発行される SQL が違が …(*゚ロ゚*)

  1.  select e.*, d.name as department_name
  2.  from employee e left outer join department d on e.department_id = d.id
  3.  where
  4.      e.salary >= 1200
  5.      and e.salary <= 1200
  6.      and e.department_id = 1200
  7.  order by e.salary

Map のサイズが1のときだけ…?

中途半端ですがコードを追いきれなかったので今日はココまでです!

Home > Tags > S2JDBC

Search
Feeds
Link
あわせて読みたいブログパーツ
Meta
AD








DMM.com 家電・日用品通販

Return to page top