2008年3月19日水曜日

Hibernateの設定


Hibernateの設定をまとめてみます。

 参考: Chapter 3. Configuration (Hibernate v.3 Documentation)

SessionFactoryの取得

すべてのマッピングファイルがパースされ後、Sessionインスタンス用のファクトリを取得しなければなりません。このファクトリはアプリケーションの全スレッドで共有されます。

SessionFactory sessions = cfg.buildSessionFactory();

Hibernateでは複数のSessionFactoryをインスタンス化でき、これは複数データベースを利用する際に便利です。

JDBCコネクション

データベースへのアクセスが必要な処理を行う際にコネクションプールからJDBCコネクションを自動取得するには、JDBCコネクションを設定する必要があります。
主なプロパティは
  • hibernate.connection.driver_class
    jdbc driver class
  • hibernate.connection.url
    jdbc URL
  • hibernate.connection.username
    database user
  • hibernate.connection.password
    database user password
  • hibernate.connection.pool_size
    maximum number of pooled connections

Hibernateのコネクションプール用アルゴリズムは必要最小限なもので、製品やパフォーマンステスト向けのものではありません。その場合HibernateのlibディレクトリにあるC3P0などを利用してください。
C3P0をコネクションプールとして利用する場合は、以下のようなhibernate.c3p0.*プロパティを設定する必要があります。
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

必須ではない設定
  • hibernate.dialect (方言)
    特定のリレーショナルデータベース向けに最適化されたSQLを発行するために、クラス名を指定する。
    (PostgreSQL: org.hibernate.dialect.PostgreSQLDialect、MySQL: org.hibernate.dialect.MySQLDialect eg...)

  • hibernate.show_sql
    コンソールにSQLを出力するか否かを指定する。(true|false)
  • hibernate.format_sql
    コンソールやログにSQLを出力する際に成型するか否か。(true|false)
  • hibernate.max_fetch_depth
    1対1や多対1関連に対してouterジョインをする際のどこまで辿るかを指定する。推奨は0(辿らない)~3。

0 件のコメント: