Serviceクラスを作成して、テストを実行する includesとexcludesNull

本家サイトをみていると、便利なメソッドがあります。

includesというメソッドを使用すると、指定した属性のみ更新してくれます。
挿入も同じように処理ができます。

excludesNullというメソッドはEntityにNullを設定していれば、Null以外の属性を更新してくれます。挿入も同じように処理ができます。

では、まずはexcludesNullを試してみます。

ServiceClass

public int updateTragetData(OrderVoucher entity) {
return this.jdbcManager.update(entity).excludesNull().execute();
}

テストクラス

public void testUpdateTargetTx() throws Exception {
OrderVoucher entity = new OrderVoucher();
entity.id = Long.parseLong("6");
entity.accountId = "666";
entity.accountName = "8888";

int expected = orderVoucherService.updateTragetData(entity);
assertEquals(expected, 1);
}

いけますね。上記以外の値はデータベースのテーブルを見る限り、何も変わっていませんでした。

では、includesを試してみます。

ServiceClass

public int updateTragetData(OrderVoucher entity, CharSequence exceptFields) {
return this.jdbcManager.update(entity).includes(exceptFields).execute();
}

テストクラス

public void testUpdateTargetTx() throws Exception {
OrderVoucher entity = new OrderVoucher();
entity.id = Long.parseLong("6");
entity.accountId = "666";
entity.accountName = "8888";

CharSequence cs = "\"accountId\""+ "," + "\"accountName\"";

int expected = orderVoucherService.updateTragetData(entity, cs);
assertEquals(expected, 1);
}

あら?
エラーですね。
Update文にて、Setの所が何も設定できない、ということでした。値の設定がおかしいのでしゅうね。

では、CharSequence を変えてみます。

CharSequence cs = "\"accountId\""+ "," + "\"accountName\"";
   ↓
CharSequence cs = "accountId"+ "," + "accountName";

あらら?
同じエラーです。includesの対象の項目がうまく取得できていない、という事ですね。


まずはいきなり2つではなく、1つに戻してみます。

public void testUpdateTargetTx() throws Exception {
OrderVoucher entity = new OrderVoucher();
entity.id = Long.parseLong("6");
entity.accountId = "666";
entity.accountName = "8888";

CharSequence cs = "accountName";

int expected = orderVoucherService.updateTragetData(entity, cs);
assertEquals(expected, 1);
}

これでしたら、大丈夫でした。


今度はServiceクラスに直接複数の値を入れてみます。

ServiceClass

public int updateTragetData(OrderVoucher entity, CharSequence exceptFields) {
return this.jdbcManager.update(entity).includes("accountId","accountName").execute();
}

これも大丈夫でした。本家のサンプル通りです。

という事は、includesメソッドのパラメータに直接記述するしかないのかなぁ?と。パラメーターで渡すと、どうも上手くいかないので。。。


今回の案件では動的に更新する対象が変わるので、エンティティーに必要な値をいれて、excludesNullメソッドを実行すれば、動的に更新するという要件は満たせそうです。Entityに対して、変な値を入れてはいけない、という事を念頭において!