Spring Roo DBからのReverse Engineering

 Spring Rooの最もベーシックな使い方はroo shell上でentityコマンドを使ってEntityを作成していくやり方ですが、既にDB上にテーブルが存在していて、そのテーブルをEntityとして使用するアプリケーションを作ることもできます。

 Rooのインストール、プロジェクトの作成、DBの接続設定までは同様の手順で行います。

Spring Roo Install
[Spring Roo]プロジェクト作成, Logging&DB接続設定

 これ以降の手順は下記ページを参考に実施していきます。

Chapter 9. Incremental Database Reverse Engineering (DBRE) Add-On

 Database Reverse Engineering(DBRE)アドオンには database introspect と database reverse engineer の2つのコマンドがありますが、いずれかを初めて実行した場合にはJDBCドライバのインストールを促すプロンプトが表示されます。

com.example roo> database introspect --schema no-schema-required
Located add-ons that may offer this JDBC driver
2 found, sorted by rank; T = trusted developer; R = Roo 1.1 compatible
ID T R DESCRIPTION -------------------------------------------------------------
01 - Y 9.0.0.801_jdbc4_0001 PostgreSQL #jdbcdriver...
02 Y Y 9.0.801.0001 Postgres #jdbcdriver driverclass:org.postgresql.Driver....
--------------------------------------------------------------------------------
[HINT] use 'addon info id --searchResultId ..' to see details about a search result
[HINT] use 'addon install id --searchResultId ..' to install a specific search result, or
[HINT] use 'addon install bundle --bundleSymbolicName TAB' to install a specific add-on version
JDBC driver not available for 'org.postgresql.Driver'

 ここで addon info コマンドを使うことで検出されたドライバの詳細情報を見ることが出来ます。

com.example roo> addon info id --searchResultId 01
Name.........: spring-roo-postgres-jdbc4-wrapper
BSN..........: org.postgresql.roo.wrapper.postgresql
Version......: 9.0.0.801_jdbc4_0001
Roo Version..: 1.1
Ranking......: 0.0
JAR Size.....: 546943 bytes
PGP Signature: 0x36673F56 signed by Ingo Thierack (ingothierack@googlemail.com)
OBR URL......: http://spring-roo-postgres-jdbc4-wrapper.googlecode.com/svn/repo/
               repository.xml
JAR URL......: httppgp://spring-roo-postgres-jdbc4-wrapper.googlecode.com/svn/re
               po/org/postgresql/roo/wrapper/org.postgresql.roo.wrapper.postgres
               ql/9.0-801.jdbc4.0001/org.postgresql.roo.wrapper.postgresql-9.0-8
               01.jdbc4.0001.jar
Description..: PostgreSQL #jdbcdriver driverclass:org.postgresql.Driver. This
               bundle wraps the standard Maven artifact:
               postgresql-9.0-801.jdbc4.

 最新のドライバをインストールするために、下記コマンドを実行します。

com.example roo> addon install id --searchResultId 01
Download URL 'http://spring-roo-postgres-jdbc4-wrapper.googlecode.com/svn/repo/org/postgresql/roo/wrapper/org.postgresql
.roo.wrapper.postgresql/9.0-801.jdbc4.0001/org.postgresql.roo.wrapper.postgresql-9.0-801.jdbc4.0001.jar' failed
This resource was signed with PGP key ID '0x36673F56', which is not currently trusted
Use 'pgp key view' to view this key, 'pgp trust' to trust it, or 'pgp automatic trust' to trust any keys
Target resource(s):
-------------------
   spring-roo-postgres-jdbc4-wrapper (9.0.0.801_jdbc4_0001)

Deploying...done.

Unable to install add-on: spring-roo-postgres-jdbc4-wrapper [version: 9.0.0.801_jdbc4_0001]

 おや、インストールに失敗してしまいました。どうやらドライバのダウンロードURLのKeyが信頼されていないということのようです。pgp key view コマンドを使用してkeyの情報を表示します。

com.example roo> pgp key view --keyId 0x36673F56
>>>> KEY ID: 0x36673F56 <<<<
     More Info: http://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=index&search=0x36673F56
     Created: 2011-1-30 18:36:08 +0000
     Fingerprint: dc1a2679fc6a938d6681a61389a2afd036673f56
     Algorithm: RSA_GENERAL
     User ID: Ingo Thierack <ingothierack@googlemail.com>
          Signed By: Key 0x36673F56 - not locally trusted
     Subkey ID: 0x1A2EDAED [RSA_GENERAL]

 このKeyを信頼してダウンロードを可能にするために、下記コマンドを実行します。

com.example roo> pgp trust --keyId 0x36673F56
Added trust for key:
>>>> KEY ID: 0x36673F56 <<<<
     More Info: http://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=index&search=0x36673F56
     Created: 2011-1-30 18:36:08 +0000
     Fingerprint: dc1a2679fc6a938d6681a61389a2afd036673f56
     Algorithm: RSA_GENERAL
     User ID: Ingo Thierack <ingothierack@googlemail.com>
          Signed By: Key 0x36673F56 (Ingo Thierack <ingothierack@googlemail.com>)
     Subkey ID: 0x1A2EDAED [RSA_GENERAL]

 そして再度インストールコマンドを実行します。

com.example roo> addon install id --searchResultId 01
Target resource(s):
-------------------
   spring-roo-postgres-jdbc4-wrapper (9.0.0.801_jdbc4_0001)

Deploying...done.

Successfully installed add-on: spring-roo-postgres-jdbc4-wrapper [version: 9.0.0.801_jdbc4_0001]
[Hint] Please consider rating this add-on with the following command:
[Hint] addon feedback bundle --bundleSymbolicName org.postgresql.roo.wrapper.postgresql --rating ... --comment "..."

 無事にインストールできました。次にDBREアドオンのdatabase introspectコマンドでテーブルの情報を表示します。

com.example roo> database introspect --schema public --file --enableViews

 出力内容は割愛しますが、XML形式でスキーマの情報が表示されます。

 そして実際にテーブル情報からEntityを作成するためにdatabase reverse engineerコマンドを実行します。

com.example roo> database reverse engineer --schema public --package ~.domain --testAutomatically
Created SRC_MAIN_RESOURCES\dbre.xml
Updated ROOT\pom.xml
Updated SRC_MAIN_RESOURCES\META-INF\persistence.xml
Created SRC_MAIN_JAVA\com\example\domain
Created SRC_MAIN_JAVA\com\example\domain\Access.java
Created SRC_MAIN_JAVA\com\example\domain\Content.java
Created SRC_TEST_JAVA\com\example\domain
Created SRC_TEST_JAVA\com\example\domain\AccessDataOnDemand.java
Created SRC_TEST_JAVA\com\example\domain\AccessIntegrationTest.java
Created SRC_TEST_JAVA\com\example\domain\ContentDataOnDemand.java
Created SRC_TEST_JAVA\com\example\domain\ContentIntegrationTest.java
Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_Configurable.aj
Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_Entity.aj
Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_DbManaged.aj
Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_ToString.aj
Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_Configurable.aj
Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_Entity.aj
Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_DbManaged.aj
Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_ToString.aj
Created SRC_TEST_JAVA\com\example\domain\AccessDataOnDemand_Roo_Configurable.aj
Created SRC_TEST_JAVA\com\example\domain\ContentDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA\com\example\domain\AccessDataOnDemand_Roo_DataOnDemand.aj
Created SRC_TEST_JAVA\com\example\domain\ContentIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA\com\example\domain\ContentIntegrationTest_Roo_IntegrationTest.aj
Created SRC_TEST_JAVA\com\example\domain\ContentDataOnDemand_Roo_Configurable.aj
Created SRC_TEST_JAVA\com\example\domain\AccessIntegrationTest_Roo_Configurable.aj
Created SRC_TEST_JAVA\com\example\domain\AccessIntegrationTest_Roo_IntegrationTest.aj

 --schemaオプションで対象のDBスキーマを、--packageオプションでEntityを作成するパッケージを指定します。--testAutomaticallyを指定することでEntityのインテグレーションテストが自動的に作成されます。

 また、--excludeTablesオプションや--includeTablesオプションで対象のテーブルを特定のテーブルに限定することも出来ます。

 これ以降の手順は、roo shellからEntityを作成したあとと同じ手順でWebアプリケーションのセットアップなどを行うことが出来ます。