手元に環境があったので、ちょっと古いのですが、JBoss5.0.1 + Seam 2.1.1でDBアクセスをするSeamアプリを作成する方法について確認をしてみました。Mac OS X で動作させています。
次のような環境を用意しています。workspace/seam-genはプロジェクトを置くための作業用ディレクトリです。
/Users/user001/Desktop/workspace/seam-gen /Users/user001/applications/jboss-5.0.1.GA /Users/user001/applications/jboss-seam-2.1.1.GA
前の記事を参考にして、sampleproject.ear などをデプロイしてある場合は、削除しておいてください。
rm -fr ~/applications/jboss-5.0.1.GA/server/default/deploy/sampleproject*
seam setup を次のようにすると、対話的に値を指定できます。これで、seam-gen/build.propertiesファイルが生成されます。
$ cd ~/applications/jboss-seam-2.1.1.GA/
$ chmod 755 seam
$ JAVA_HOME=/Library/Java/Home ./seam setup
SEAM_HOME: .
Using seam-gen sources from: ./seam-gen
Buildfile: ./seam-gen/build.xml
init:
setup:
[echo] Welcome to seam-gen :-)
[input] Enter your Java project workspace (the directory that contains your Seam projects) [C:/Projects] [C:/Projects]
/Users/user001/Desktop/workspace/seam-gen
[input] Enter your JBoss AS home directory [C:/Program Files/jboss-4.2.3.GA] [C:/Program Files/jboss-4.2.3.GA]
/Users/user001/applications/jboss-5.0.1.GA
[input] Enter the project name [myproject] [myproject]
sampledbproject
[echo] Accepted project name as: sampledbproject
[input] Do you want to use ICEfaces instead of RichFaces [n] (y, [n])
[input] skipping input as property icefaces.home.new has already been set.
[input] Select a RichFaces skin [classic] (blueSky, [classic], deepMarine, DEFAULT, emeraldTown, japanCherry, ruby, wine)
blueSky
[input] Is this project deployed as an EAR (with EJB components) or a WAR (with no EJB support) [ear] ([ear], war)
[input] Enter the Java package name for your session beans [com.mydomain.sampledbproject] [com.mydomain.sampledbproject]
com.example.sampledbproject.session
[input] Enter the Java package name for your entity beans [com.example.sampledbproject.session] [com.example.sampledbproject.session]
com.example.sampledbproject.entity
[input] Enter the Java package name for your test cases [com.example.sampledbproject.session.test] [com.example.sampledbproject.session.test]
com.example.sampledbproject.test
[input] What kind of database are you using? [hsql] ([hsql], mysql, oracle, postgres, mssql, db2, sybase, enterprisedb, h2)
[input] Enter the Hibernate dialect for your database [org.hibernate.dialect.HSQLDialect] [org.hibernate.dialect.HSQLDialect]
[input] Enter the filesystem path to the JDBC driver jar [/Users/user001/Applications/jboss-seam-2.1.1.GA/lib/hsqldb.jar] [/Users/user001/Applications/jboss-seam-2.1.1.GA/lib/hsqldb.jar]
/Users/user001/applications/jboss-seam-2.1.1.GA/lib/hsqldb.jar
[input] Enter JDBC driver class for your database [org.hsqldb.jdbcDriver] [org.hsqldb.jdbcDriver]
[input] Enter the JDBC URL for your database [jdbc:hsqldb:.] [jdbc:hsqldb:.]
jdbc:hsqldb:hsql://localhost:1701/
[input] Enter database username [sa] [sa]
[input] Enter database password [] []
[input] Enter the database schema name (it is OK to leave this blank) [] []
[input] Enter the database catalog name (it is OK to leave this blank) [] []
[input] Are you working with tables that already exist in the database? [n] (y, [n])
y
[input] Do you want to drop and recreate the database tables and data in import.sql each time you deploy? [n] (y, [n])
n
[propertyfile] Creating new property file: /Users/user001/Applications/jboss-seam-2.1.1.GA/seam-gen/build.properties
[echo] Installing JDBC driver jar to JBoss AS
[echo] Type './seam create-project' to create the new project
BUILD SUCCESSFUL
Total time: 1 minute 52 seconds
HSQLDBをサーバーモードで稼働させ、jdbc:hsqldb:hsql://localhost:1701/ でアクセスできるようにしておく必要があります。
できあがるファイルは「/Users/user001/Applications/jboss-seam-2.1.1.GA/seam-gen/build.properties」ではなく、「/Users/user001/applications/jboss-seam-2.1.1.GA/seam-gen/build.properties」です。なぜApplicationsと表示されるのかは謎。
#Generated by seam setup #Tue Jun 19 10:55:08 JST 2012 hibernate.connection.password= workspace.home=/Users/user001/Desktop/workspace/seam-gen model.package=com.example.sampledbproject.entity driver.jar=/Users/user001/Applications/jboss-seam-2.1.1.GA/lib/hsqldb.jar action.package=com.example.sampledbproject.session test.package=com.example.sampledbproject.test database.type=hsql richfaces.skin=blueSky hibernate.default_catalog.null= hibernate.default_schema.null= database.drop=n project.name=sampledbproject hibernate.connection.username=sa hibernate.connection.driver_class=org.hsqldb.jdbcDriver hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider project.type=ear icefaces.home= database.exists=y jboss.home=/Users/user001/applications/jboss-5.0.1.GA hibernate.dialect=org.hibernate.dialect.HSQLDialect hibernate.connection.url=jdbc\:hsqldb\:hsql\://localhost\:1701/ icefaces=n
HSQLDBをサーバモードで起動するためにhsqldb-ds.xml, bindings.xml
の修正が必要なので、standaloneモードからserverモードへ変更。念のためバックアップはとっておく。
$ cp ~/applications/jboss-5.0.1.GA/server/default/deploy/hsqldb-ds.xml ~/applications/jboss-5.0.1.GA/server/default/deploy/hsqldb-ds.xml.original $ cp ~/applications/jboss-5.0.1.GA/server/default/conf/bootstrap/bindings.xml ~/applications/jboss-5.0.1.GA/server/default/conf/bootstrap/bindings.xml.original
次のように修正。
$ cat ~/applications/jboss-5.0.1.GA/server/default/deploy/hsqldb-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>DefaultDS</jndi-name>
<connection-url>jdbc:hsqldb:hsql://${jboss.bind.address}:1701</connection-url>
<driver-class>org.hsqldb.jdbcDriver</driver-class>
<user-name>sa</user-name>
<password></password>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>0</idle-timeout-minutes>
<track-statements/>
<security-domain>HsqlDbRealm</security-domain>
<prepared-statement-cache-size>32</prepared-statement-cache-size>
<metadata>
<type-mapping>Hypersonic SQL</type-mapping>
</metadata>
<depends>jboss:service=Hypersonic</depends>
</local-tx-datasource>
<mbean code="org.jboss.jdbc.HypersonicDatabase"
name="jboss:service=Hypersonic">
<attribute name="Port">
<value-factory bean="ServiceBindingManager" method="getIntBinding"
parameter="jboss:service=Hypersonic"/>
</attribute>
<attribute name="BindAddress">${jboss.bind.address}</attribute>
<attribute name="Silent">true</attribute>
<attribute name="Database">default</attribute>
<attribute name="Trace">false</attribute>
<attribute name="No_system_exit">true</attribute>
</mbean>
</datasources>
$ cat ~/applications/jboss-5.0.1.GA/server/default/conf/bootstrap/bindings.xml
略
<!-- Commented out as tcp/ip access to Hypersonic is not enabled by default -->
<bean class="org.jboss.services.binding.ServiceBindingMetadata">
<property name="serviceName">jboss:service=Hypersonic</property>
<property name="port">1701</property>
</bean>
略
これで、JBossが起動すると、HSQLDBがサーバーモードで起動するようになります。確認のため、JBossを起動します。
$ /Users/user001/applications/jboss-5.0.1.GA/bin/run.sh
org.hsqldb.util.DatabaseManagerを使って、HSQLDBのサーバーモードへ接続し、データベースの準備をします。スクリプトを用意しておきます。まずは、ディレクトリを用意します。
$ mkdir ~/applications/jboss-5.0.1.GA/server/default/tool/
次のdbm.shスクリプトを作成して、実行可能としてから、実行します。
$ cat ~/applications/jboss-5.0.1.GA/server/default/tool/dbm.sh
#!/bin/sh
JBOSS_HOME="/Users/user001/applications/jboss-5.0.1.GA"
java -cp ${JBOSS_HOME}/common/lib/hsqldb.jar org.hsqldb.util.DatabaseManager
$ chmod 755 ~/applications/jboss-5.0.1.GA/server/default/tool/dbm.sh
$ ~/applications/jboss-5.0.1.GA/server/default/tool/dbm.sh
org.hsqldb.util.DatabaseManagerのGUIが表示されるので、下記のように指定して、[OK]をクリックして接続します。
Setting Name: sampledb Type: HSQL Database Engine Server Driver: org.hsqldb.jdbcDriver URL: jdbc:hsqldb:hsql://localhost:1701/ User: sa Password:
SQL文を実行するところで、下記のようにして、テーブルを2つ用意。また、データを入れておきます。
create table items_list ( id int, name varchar(30) not null, primary key(id) ); create table item ( id int, items_list_id int not null, name varchar(30) not null, primary key(id), foreign key(items_list_id) references items_list(id) ); insert into items_list values(1, 'list1'); insert into items_list values(2, 'list2'); insert into item values(1, 2, 'item10'); insert into item values(2, 2, 'item11'); insert into item values(3, 1, 'item20'); insert into item values(4, 1, 'item21');
ここまでで、準備ができたので、次のようにしてプロジェクトを生成します。
$ JAVA_HOME=/Library/Java/Home ./seam create-project
無事生成されると、それでもう準備OKです。/Users/user001/Desktop/workspace/seam-gen/sampledbproject/ に必要なファイルができあがっているはずです。Eclipseへインポートすることもできますし、NetBeansへインポートすることもできます。
略
create-project:
[echo] A new Seam project named 'sampledbproject' was created in the /Users/user001/Desktop/workspace/seam-gen directory
[echo] Type './seam explode' and go to http://localhost:8080/sampledbproject
[echo] Eclipse Users: Import the project using File > Import... > Existing Projects into Workspace, set the root directory to /Users/user001/Desktop/workspace/seam-gen, then select the project named sampledbproject
[echo] NetBeans Users: Open the project using File > Open project... and select the project folder /Users/user001/Desktop/workspace/seam-gen/sampledbproject
[echo] IDEA Users: Open the project using File > Open project... and select the file /Users/user001/Desktop/workspace/seam-gen/sampledbproject/sampledbproject.ipr
BUILD SUCCESSFUL
Total time: 14 seconds
とりあえず、次のようにしてseam explodeすると、JBossへ展開した形でファイルがデプロイされます。動作確認をするには、JBossを起動してから、http://localhost:8080/sampledbproject へWebブラウザからアクセスしてみます。ログインには、admin, パスワードなし を指定します。
$ JAVA_HOME=/Library/Java/Home ./seam explode 略 BUILD SUCCESSFUL Total time: 2 seconds $ /Users/user001/applications/jboss-5.0.1.GA/bin/run.sh
画面が表示できることを確認したら、seamコマンドでDBテーブルからプログラムと画面を自動生成してみます。成功すると、画面などができあがります。
$ JAVA_HOME=/Library/Java/Home ./seam generate
略
[javaformatter] Java formatting of 27 files completed. Skipped 0 file(s).
[echo] Type './seam restart' and go to http://localhost:8080/sampledbproject
generate:
BUILD SUCCESSFUL
Total time: 12 seconds
seamコマンドでrestartしてから、http://localhost:8080/sampledbproject へアクセスし、Homeなどをクリックすると画面ができていることがわかります。
$ JAVA_HOME=/Library/Java/Home ./seam restart
http://localhost:8080/sampledbproject/ItemsListList.seam や、http://localhost:8080/sampledbproject/ItemList.seam へアクセスするとDBのデータを操作できるようになっています。簡単なデータベースアプリなら、これで実装ができてしまいます。