Hibernate EntityManagerとMySQLを準備
手元には Windows版 MySQL4.0.x があったので、とりあえずソレを利用しました。Hibernate CoreとEntityManager、MySQLのConnector/J(JDBC)をダウンロードして、次のように配置します。
/hibernate-3.2
/hibernate-entitymanager-3.2.0.GA(作業ディレクトリ)
+ /jdbc/mysql-connector-java-5.0.4-bin.jar
Ant/jUnitは、Hibernate Coreに同梱されてます。作業ディレクトリに、次のような内容の build.batを作りました。
build.bat:
java -Xms64m -Xmx256m \
-cp "../hibernate-3.2/lib/ant-launcher-1.6.5.jar" \
org.apache.tools.ant.launch.Launcher -lib lib %1 %2 %3 %4 %5
テスト用のデータベースを作成。名前、ユーザー・パスワードは、全て"hibernate"です。
> mysql -u root
mysql> create database hibernate;
mysql> grant all on hibernate.* to hibernate@localhost
identified by 'hibernate'
mysql> flush privileges;
テストケースの実行
デフォルトのテストコンフィグレーションでは、HSQLを使うようになっているので、MySQLにきりかえます。
まずは、./test/hibernate.propertiesを修正。
hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql://localhost/hibernate
hibernate.connection.username hibernate
hibernate.connection.password hibernate
dialectにMySQLInnoDBDialect/MySQLMyISAMDialectを指定すると、create table時に ENGINE を指定してくれます。MySQLDialectは、デフォルト(my.cnf:default-table-type)を選択するようです。ここではとりあえず、デフォルトのまま、MyISAMエンジンを使うことにします。
上記の環境に合わせて、以下のクソXML!を書き換えます。
./test/org/hibernate/ejb/test/hibernate.cfg.xml
./test-resources/cfgxmlpar/org/hibernate/ejb
+/test/pack/cfgxmlpar/hibernate.cfg.xml
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">
hibernate
</property>
<property name="hibernate.connection.password">
hibernate
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernate
</property>
./test-resources/*/META-INF/persistence.xml(5つくらいあります)
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username"
value="hibernate"/>
<property name="hibernate.connection.password"
value="hibernate"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost/hibernate"/>
ここまで準備ができたら、実行します。
> build junit junitreport
テストレポートは、./test_output/index.html に出力されます。
結果

テストカバレージは、87%ですが、エラーの出方は主に2つです。
①シーケンス取得のところで、
org.hibernate.MappingException:
could not instantiate id generator
②バッチ更新とLockテストで、SQLシンタックスエラー。
org.hibernate.exception.SQLGrammarException
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException
MySQL4.0では、さもありなん。InnoDBにすると解消するかと思い、MySQLInnoDBDialectを設定してみましたが、変わりませんでした。
次回は、MySQL4.1/5.0あたりもやってみて、結果をマトリクスにしてみたいと思います。
同様に、PostgreSQL8.1.5 の結果

同様に、Windows版 Postgres8.1.5、jdbc2eeで試しました。
hibernate.dialect org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class org.postgresql.Driver
hibernate.connection.url jdbc:postgresql://localhost:5432/hibernate
hibernate.connection.username hibernate
hibernate.connection.password hibernate
カバレージは95%ですが、エラーの傾向はMySQLとは違うようです。
開発スタート前に目処をつける
以上の作業をやってみて、コミュニティ側が用意しているTestCaseは、一度は実行してみるべきだと感じました。
開発スタート前に課題がわかりますし、開発中に発生するであろうExceptionにも免疫力がつくので、怖くない。動かない機能を知り、業務要件と照らし合わせて、フルイにかける。これで十分な気がします。
(カバレージ100%にすることを目標にすると、コレは大変!そういう人はコミュニティに貢献してください。)
こんな作業は小一時間あれば済みますが、やっておくのとおかないのでは、その後の開発プロジェクト時のロスタイムに、大きな差がでるのではないでしょうか。ソフトウェアの階層構造が深くなっている昨今、スイートスポット(?)での動作確認は、あとあと効いてくるはずです。