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ドライバのインストールを促すプロンプトが表示されます。
1 com.example roo> database introspect --schema no-schema-required
2 Located add-ons that may offer this JDBC driver
3 2 found, sorted by rank; T = trusted developer; R = Roo 1.1 compatible
4 ID T R DESCRIPTION -------------------------------------------------------------
5 01 - Y 9.0.0.801_jdbc4_0001 PostgreSQL
6 02 Y Y 9.0.801.0001 Postgres
7 --------------------------------------------------------------------------------
8 [HINT] use 'addon info id --searchResultId ..' to see details about a search result
9 [HINT] use 'addon install id --searchResultId ..' to install a specific search result, or
10 [HINT] use 'addon install bundle --bundleSymbolicName TAB' to install a specific add-on version
11 JDBC driver not available for 'org.postgresql.Driver'
ここで addon info コマンドを使うことで検出されたドライバの詳細情報を見ることが出来ます。
1 com.example roo> addon info id --searchResultId 01
2 Name.........: spring-roo-postgres-jdbc4-wrapper
3 BSN..........: org.postgresql.roo.wrapper.postgresql
4 Version......: 9.0.0.801_jdbc4_0001
5 Roo Version..: 1.1
6 Ranking......: 0.0
7 JAR Size.....: 546943 bytes
8 PGP Signature: 0x36673F56 signed by Ingo Thierack (ingothierack@googlemail.com)
9 OBR URL......: http://spring-roo-postgres-jdbc4-wrapper.googlecode.com/svn/repo/
10 repository.xml
11 JAR URL......: httppgp://spring-roo-postgres-jdbc4-wrapper.googlecode.com/svn/re
12 po/org/postgresql/roo/wrapper/org.postgresql.roo.wrapper.postgres
13 ql/9.0-801.jdbc4.0001/org.postgresql.roo.wrapper.postgresql-9.0-8
14 01.jdbc4.0001.jar
15 Description..: PostgreSQL
16 bundle wraps the standard Maven artifact:
17 postgresql-9.0-801.jdbc4.
最新のドライバをインストールするために、下記コマンドを実行します。
1 com.example roo> addon install id --searchResultId 01
2 Download URL 'http://spring-roo-postgres-jdbc4-wrapper.googlecode.com/svn/repo/org/postgresql/roo/wrapper/org.postgresql
3 .roo.wrapper.postgresql/9.0-801.jdbc4.0001/org.postgresql.roo.wrapper.postgresql-9.0-801.jdbc4.0001.jar' failed
4 This resource was signed with PGP key ID '0x36673F56', which is not currently trusted
5 Use 'pgp key view' to view this key, 'pgp trust' to trust it, or 'pgp automatic trust' to trust any keys
6 Target resource(s):
7 -------------------
8 spring-roo-postgres-jdbc4-wrapper (9.0.0.801_jdbc4_0001)
9
10 Deploying...done.
11
12 Unable to install add-on: spring-roo-postgres-jdbc4-wrapper [version: 9.0.0.801_jdbc4_0001]
おや、インストールに失敗してしまいました。どうやらドライバのダウンロードURLのKeyが信頼されていないということのようです。pgp key view コマンドを使用してkeyの情報を表示します。
1 com.example roo> pgp key view --keyId 0x36673F56
2 >>>> KEY ID: 0x36673F56 <<<<
3 More Info: http://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=index&search=0x36673F56
4 Created: 2011-1-30 18:36:08 +0000
5 Fingerprint: dc1a2679fc6a938d6681a61389a2afd036673f56
6 Algorithm: RSA_GENERAL
7 User ID: Ingo Thierack <ingothierack@googlemail.com>
8 Signed By: Key 0x36673F56 - not locally trusted
9 Subkey ID: 0x1A2EDAED [RSA_GENERAL]
このKeyを信頼してダウンロードを可能にするために、下記コマンドを実行します。
1 com.example roo> pgp trust --keyId 0x36673F56
2 Added trust for key:
3 >>>> KEY ID: 0x36673F56 <<<<
4 More Info: http://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=index&search=0x36673F56
5 Created: 2011-1-30 18:36:08 +0000
6 Fingerprint: dc1a2679fc6a938d6681a61389a2afd036673f56
7 Algorithm: RSA_GENERAL
8 User ID: Ingo Thierack <ingothierack@googlemail.com>
9 Signed By: Key 0x36673F56 (Ingo Thierack <ingothierack@googlemail.com>)
10 Subkey ID: 0x1A2EDAED [RSA_GENERAL]
そして再度インストールコマンドを実行します。
1 com.example roo> addon install id --searchResultId 01
2 Target resource(s):
3 -------------------
4 spring-roo-postgres-jdbc4-wrapper (9.0.0.801_jdbc4_0001)
5
6 Deploying...done.
7
8 Successfully installed add-on: spring-roo-postgres-jdbc4-wrapper [version: 9.0.0.801_jdbc4_0001]
9 [Hint] Please consider rating this add-on with the following command:
10 [Hint] addon feedback bundle --bundleSymbolicName org.postgresql.roo.wrapper.postgresql --rating ... --comment "..."
無事にインストールできました。次にDBREアドオンのdatabase introspectコマンドでテーブルの情報を表示します。
1 com.example roo> database introspect --schema public --file --enableViews
出力内容は割愛しますが、XML形式でスキーマの情報が表示されます。
そして実際にテーブル情報からEntityを作成するためにdatabase reverse engineerコマンドを実行します。
1 com.example roo> database reverse engineer --schema public --package ~.domain --testAutomatically
2 Created SRC_MAIN_RESOURCES\dbre.xml
3 Updated ROOT\pom.xml
4 Updated SRC_MAIN_RESOURCES\META-INF\persistence.xml
5 Created SRC_MAIN_JAVA\com\example\domain
6 Created SRC_MAIN_JAVA\com\example\domain\Access.java
7 Created SRC_MAIN_JAVA\com\example\domain\Content.java
8 Created SRC_TEST_JAVA\com\example\domain
9 Created SRC_TEST_JAVA\com\example\domain\AccessDataOnDemand.java
10 Created SRC_TEST_JAVA\com\example\domain\AccessIntegrationTest.java
11 Created SRC_TEST_JAVA\com\example\domain\ContentDataOnDemand.java
12 Created SRC_TEST_JAVA\com\example\domain\ContentIntegrationTest.java
13 Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_Configurable.aj
14 Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_Entity.aj
15 Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_DbManaged.aj
16 Created SRC_MAIN_JAVA\com\example\domain\Access_Roo_ToString.aj
17 Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_Configurable.aj
18 Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_Entity.aj
19 Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_DbManaged.aj
20 Created SRC_MAIN_JAVA\com\example\domain\Content_Roo_ToString.aj
21 Created SRC_TEST_JAVA\com\example\domain\AccessDataOnDemand_Roo_Configurable.aj
22 Created SRC_TEST_JAVA\com\example\domain\ContentDataOnDemand_Roo_DataOnDemand.aj
23 Created SRC_TEST_JAVA\com\example\domain\AccessDataOnDemand_Roo_DataOnDemand.aj
24 Created SRC_TEST_JAVA\com\example\domain\ContentIntegrationTest_Roo_Configurable.aj
25 Created SRC_TEST_JAVA\com\example\domain\ContentIntegrationTest_Roo_IntegrationTest.aj
26 Created SRC_TEST_JAVA\com\example\domain\ContentDataOnDemand_Roo_Configurable.aj
27 Created SRC_TEST_JAVA\com\example\domain\AccessIntegrationTest_Roo_Configurable.aj
28 Created SRC_TEST_JAVA\com\example\domain\AccessIntegrationTest_Roo_IntegrationTest.aj
--schemaオプションで対象のDBスキーマを、--packageオプションでEntityを作成するパッケージを指定します。--testAutomaticallyを指定することでEntityのインテグレーションテストが自動的に作成されます。
また、--excludeTablesオプションや--includeTablesオプションで対象のテーブルを特定のテーブルに限定することも出来ます。
これ以降の手順は、roo shellからEntityを作成したあとと同じ手順でWebアプリケーションのセットアップなどを行うことが出来ます。