postgresqlのJSON型のサポートについて

312 views
Skip to first unread message

kondo shigenobu

unread,
Aug 21, 2016, 11:02:30 PM8/21/16
to DBFluteユーザの集い
近藤と申します。

現在、開発案件でDBFluteの導入を検討しています。

質問なのですが、DBFluteのオブジェクトマッピングでpostgresqlのJSON型は
サポートされていますでしょうか?公式サイトに特に明記がなかったのでこちら
に投稿させていただきました。
未サポートの場合、何らかの方法で対応したことのある方がいらしゃいましたら
アドバイスをいただきたく思っています。

以上、よろしくお願いします。

kubo

unread,
Aug 21, 2016, 11:10:55 PM8/21/16
to DBFluteユーザの集い
jfluteです

近藤さん、こんにちは
取り急ぎ、正直 "試したことがない" という状況なので、
自動で何かオブジェクトにマッピングされるということはないです。

推測としては、認識できないString 型になって、
parseは自前でやれば扱えないことはないんじゃないかと。

一方で、対応できてない型を、自分で対応させることもできます。
例えば、テスト環境では、ARRAY型やXML型を、
自前でマッピングロジックを用意して独自のクラスにマッピングしています。
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/tree/master/src/main/java/org/docksidestage/postgresql/mytype
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/blob/master/dbflute_maihamadb/dfprop/typeMappingMap.dfprop#L24

なので、スムーズに利用できる機能はありませんが、
全く使えないことはないという感じですね。

kubo

unread,
Aug 21, 2016, 11:26:13 PM8/21/16
to DBFluteユーザの集い
jfluteです

五月雨で検証結果投げていきますね。
PostgreSQL-9.2 にて、JSON型で自動生成したら、
(認識できない型のデフォルトとして)String型になりました。

で、Insertしてみたのですが、例外になってしまいましたね...。

// ## Arrange ##
VendorCheck vendorCheck = createVendorCheck(88881);
vendorCheck.setTypeOfJson("{\"sea\": \"mystic\", \"land\": \"oneman\"}");

// ## Act ##
vendorCheckBhv.insert(vendorCheck);

Caused by: org.postgresql.util.PSQLException: ERROR: column
"type_of_json" is of type json but expression is of type character
varying
ヒント: You will need to rewrite or cast the expression.
ポジション: 71

MySQLだとわりと文字列で処理してくれたりするのですが、
PostgreSQLは全体的に型が厳しいので(良いことではあるけど)、
ちゃんと MyJSON 型を作らないとですね。

kubo

unread,
Aug 21, 2016, 11:59:31 PM8/21/16
to DBFluteユーザの集い
jfluteです

MyJSON 型を作ることで動作しました。
(insert, select)

// MyJSON (Entityのプロパティの型になるクラス)
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/blob/master/src/main/java/org/docksidestage/postgresql/mytype/MyJSON.java

// MyTypeOfJSON (JDBCとやり取りするクラス)
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/blob/master/src/main/java/org/docksidestage/postgresql/mytype/valuetype/MyTypeOfJSON.java

// databaseInfoMap.dfprop の typeMappingMap
// (これで MyJSON で自動生成される)
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/blob/master/dbflute_maihamadb/dfprop/databaseInfoMap.dfprop#L113

// ExtendedDBFluteInitializer
// (これで、MyJSONに対して MyTypeOfJSON が利用される)
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/blob/master/src/main/java/org/docksidestage/postgresql/dbflute/nogen/ExtendedDBFluteInitializer.java#L24

// littleAdjustmentMap.dfprop の extendedDBFluteInitializerClass
// (その ExtendedDBFluteInitializer をDBFluteに登録にする)
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/blob/master/dbflute_maihamadb/dfprop/littleAdjustmentMap.dfprop#L269


動作確認のためのテストメソッド:
https://github.com/dbflute-test/dbflute-test-dbms-postgresql/blob/master/src/test/java/org/docksidestage/postgresql/dbflute/vendor/VendorDataTypeTest.java#L951

public void test_JSON_of_varchar_type_select() throws Exception {
// ## Arrange ##
VendorCheck vendorCheck = createVendorCheck(88881);
vendorCheck.setTypeOfJson(new MyJSON().setup("{\"sea\":
\"mystic\", \"land\": \"oneman\"}"));
// Caused by: org.postgresql.util.PSQLException: ERROR: column
// "type_of_json" is of type json but expression is of type
character varying
// hint: You will need to rewrite or cast the expression.
//vendorCheck.setTypeOfJson("{\"sea\": \"mystic\", \"land\": \"oneman\"}");

// ## Act ##
vendorCheckBhv.insert(vendorCheck);

// ## Assert ##
// Basically unsupported type on DBFlute
VendorCheckCB cb = new VendorCheckCB();
cb.query().setVendorCheckId_Equal(88881L);
VendorCheck actual = vendorCheckBhv.selectEntityWithDeletedCheck(cb);
MyJSON json = actual.getTypeOfJson();
assertEquals("{\"sea\": \"mystic\", \"land\": \"oneman\"}",
json.toString());
}


DBFluteでデフォルトで対応できなくはないですが、
MyJSONクラスをどういうものにするのが最適なのか、
ちょっと固まらないですね…
結局、MyJSONクラスをアプリで好きなように
変更したくなるんじゃないかと思えるので。

例えば、実際にアプリで使う時は、
MyJSONの中にGSONとか忍び込ませて...

ProductJsonBean jsonBean = json.mappingTo(ProductJsonBean.class);

というように、やりたいですよね、きっと。
(Validationもかけたくなるかなって)

kondo shigenobu

unread,
Aug 23, 2016, 4:41:39 AM8/23/16
to DBFluteユーザの集い
jfluteさん

お世話になります。近藤です。
検証までご丁寧にありがとうございます。
頂いた内容で試してみます。また何かありましたらよろしくお願いします。



2016年8月22日月曜日 12時59分31秒 UTC+9 jflute:
Reply all
Reply to author
Forward
0 new messages