HIBERNATE - Relational Persistence for Idiomatic Java
Hibernate Reference Documentation
Hibernateレファレンスドキュメンテーション
3.2.2
Table of Contents(コンテンツテーブル)
Preface(序文)
1. Introduction to Hibernate(Hibernateの紹介)
2. Architecture(アーキテクチャ)
3. Configuration(設定)
4. Persistent Classes(永続クラス)
6. Collection Mapping(コレクションのマッピング)
7. Association Mappings(関連マッピング)
8. Component Mapping(コンポーネントのマッピング)
9. Inheritance Mapping(継承マッピング)
10. Working with objects(オブジェクトを扱う)
11. Transactions And Concurrency(トランザクションと並行性)
12. Interceptors and events(インターセプタとイベント)
13. Batch processing(バッチ処理)
15. Criteria Queries(Criteriaクエリ)
17. Filtering data(データのフィルターリング)
19. Improving performance(パフォーマンスの改善)
20. Toolset Guide(ツールセットガイド)
20.1.1. Customizing the schema(スキーマのカスタマイズ)
20.1.2. Running the tool(ツールの実行)
20.1.3. Properties(プロパティ)
20.1.4. Using Ant(Antをしようする)
20.1.5. Incremental schema updates(インクリメンタルなスキーマ更新)
20.1.6. Using Ant for incremental schema updates(インクリメンタルなスキーマ更新に対するAntの使用)
20.1.7. Schema validation()
20.1.8. Using Ant for schema validation(スキーマのバリデーションにAntを使用する)
Chapter 20. Toolset Guide(第20章:ツールセットガイド)
Roundtrip engineering with Hibernate is possible using a set of Eclipse plugins, commandline tools, as well as Ant tasks.
Hibernateを使ったラウンドトリップエンジニアリングは、Eclipseプラグインやコマンドラインツール、もちろんAntタスクを使うことで可能です。
The Hibernate Tools currently include plugins for the Eclipse IDE as well as Ant tasks for reverse engineering of existing databases:
Hibernate Tools は現在、既存データベースのリバースエンジニアリングのAntタスクに加えて、EclipseIDEのプラグインを含みます。
? Mapping Editor: An editor for Hibernate XML mapping files, supporting auto-completion and syntax highlighting. It also supports semantic auto-completion for class names and property/field names, making it much more versatile than a normal XML editor.
マッピングエディタ: HibernateのXMLマッピングファイル用のエディタで、自動補完と構文強調表示をサポートしています。クラス名やプロパティ/フィールド名に対する自動補完もサポートし、通常のXMLエディタよりも強力です。
? Console: The console is a new view in Eclipse. In addition to a tree overview of your console configurations, you also get an interactive view of your persistent classes and their relationships. The console allows you to execute HQL queries against your database and browse the result directly in Eclipse.
Console: コンソールはエクリプスの新しいビューです。コンソールコンフィギュレーションのツリーオーバービューに加えて、永続クラスとその関連の相互作用ビューも得られます。データベースにHQLを実行し、結果を直接エクリプス上で見ることができます。
? Development Wizards: Several wizards are provided with the Hibernate Eclipse tools; you can use a wizard to quickly generate Hibernate configuration (cfg.xml) files, or you may even completely reverse engineer an existing database schema into POJO source files and Hibernate mapping files. The reverse engineering wizard supports customizable templates.
開発ウィザードHibernateのEclipseツールはいくつかのウィザードを提供します。ウィザードを使ってHibernateの設定ファイル(cfg.xml)をすばやく生成したり、既存のデータベーススキーマをPOJOのソースファイルとHibernateのマッピングファイルへと、完全にリバースエンジニアリングすることができます。リバースエンジニアリングウィザードはカスタマイズ可能なテンプレートをサポートします。
? Ant Tasks:
Antタスク
Please refer to the Hibernate Tools package and it's documentation for more information.
より詳しい情報は Hibernate Tools パッケージとそのドキュメントを参照してください。
However, the Hibernate main package comes bundled with an integrated tool (it can even be used from "inside" Hibernate on-the-fly): SchemaExport aka hbm2ddl.
しかし、Hibernateのメインパッケージは SchemaExport 、別名 hbm2ddl も含みます(Hibernate内でオンザフライ使用できます)。
20.1. Automatic schema generation(スキーマの自動生成)
DDL may be generated from your mapping files by a Hibernate utility. The generated schema includes referential integrity constraints (primary and foreign keys) for entity and collection tables. Tables and sequences are also created for mapped identifier generators.
DDLはHibernateユーティリティによりマッピングファイルから生成することができます。生成されたスキーマはエンティティやコレクションのテーブルに対する参照整合性制約(主キーと外部キー)を含みます。テーブルとシーケンスはマッピングする識別子ジェネレータに対して生成されます。
You must specify a SQL Dialect via the hibernate.dialect property when using this tool, as DDL is highly vendor specific.
DDLはベンダー依存なので、このツールを使うときは、hibernate.dialect プロパティでSQLの 方言 を指定 しなければなりません 。
First, customize your mapping files to improve the generated schema.
まず、生成されるスキーマを改善するように、マッピングファイルをカスタマイズしてください。
20.1.1. Customizing the schema(スキーマのカスタマイズ)
Many Hibernate mapping elements define optional attributes named length, precisionand scale. You may set the length, precision and scale of a column with this attribute.
多くのHibernateのマッピング要素では、オプションの length という名の属性を定義しています。この属性でカラム長を設定することができます(またはNUMERIC/DECIMAL型のデータの精度を設定できます)。
<property name="zip" length="5"/>
<property name="balance" precision="12" scale="2"/>
Some tags also accept a not-null attribute (for generating a NOT NULL constraint on table columns) and a unique attribute (for generating UNIQUE constraint on table columns).
not-null 属性(テーブルのカラムへ NOT NULL 制約を生成する)と unique 属性(テーブルのカラムへ UNIQUE 制約を生成する)が設定できるタグもあります。
<many-to-one name="bar" column="barId" not-null="true"/>
<element column="serialNumber" type="long" not-null="true" unique="true"/>
A unique-key attribute may be used to group columns in a single unique key constraint. Currently, the specified value of the unique-key attribute is not used to name the constraint in the generated DDL, only to group the columns in the mapping file.
unique-key 属性はカラムをグループ化して一つのキー制約にするために使われます。現在、unique-key 属性で指定された値は制約の指定には 使われず 、マッピングファイルでカラムをグループ化することにのみ使われます。
<many-to-one name="org" column="orgId" unique-key="OrgEmployeeId"/>
<property name="employeeId" unique-key="OrgEmployee"/>
An index attribute specifies the name of an index that will be created using the mapped column or columns. Multiple columns may be grouped into the same index, simply by specifying the same index name.
index 属性はマッピングするカラムを使って生成したインデックスの名前を指定します。複数カラムを1つのインデックスにグループ化できます。単に、同じインデックス名を指定するだけです。
<property name="lastName" index="CustName"/>
<property name="firstName" index="CustName"/>
A foreign-key attribute may be used to override the name of any generated foreign key constraint.
foreign-key 属性は、生成された外部キー制約の名前をオーバーライドするために使用できます。
<many-to-one name="bar" column="barId" foreign-key="FKFooBar"/>
Many mapping elements also accept a child <column> element. This is particularly useful for mapping multi-column types:
多くのマッピング要素は、子 <column> 要素を記述できます。これは複数カラム型のマッピングには特に有用です。
<property name="name" type="my.customtypes.Name"/>
<column name="last" not-null="true" index="bar_idx" length="30"/>
<column name="first" not-null="true" index="bar_idx" length="20"/>
<column name="initial"/>
</property>
The default attribute lets you specify a default value for a column (you should assign the same value to the mapped property before saving a new instance of the mapped class).
default 属性はカラムのデフォルト値を指定します(マッピングしたクラスの新しいインスタンスを保存する前に、マッピングしたプロパティへ同じ値を代入すべきです)。
<property name="credits" type="integer" insert="false">
<column name="credits" default="10"/>
</property>
<version name="version" type="integer" insert="false">
<column name="version" default="0"/>
</property>
The sql-type attribute allows the user to override the default mapping of a Hibernate type to SQL datatype.
sql-type 属性で、デフォルトのHibernate型からSQLのデータ型へのマッピングをオーバーライドできます。
<property name="balance" type="float">
<column name="balance" sql-type="decimal(13,3)"/>
</property>
The check attribute allows you to specify a check constraint.
check 属性でチェック制約を指定することができます。
<property name="foo" type="integer">
<column name="foo" check="foo > 10"/>
</property>
<class name="Foo" table="foos" check="bar < 100.0">
...
<property name="bar" type="float"/>
</class>
Table 20.1. Summary(まとめ)
Attribute(属性)
|
Values(値)
|
Interpretation(説明)
|
length
|
number
|
column length
|
precision
|
number
|
column decimal precision
|
scale
|
number
|
column decimal scale
|
not-null
|
true|false
|
specfies that the column should be non-nullable
|
unique
|
true|false
|
specifies that the column should have a unique constraint
|
index
|
index_name
|
specifies the name of a (multi-column) index
|
unique-key
|
unique_key_name
|
specifies the name of a multi-column unique constraint
|
foreign-key
|
foreign_key_name
|
specifies the name of the foreign key constraint generated for an association, for a <one-to-one>, <many-to-one>, <key>, or <many-to-many> mapping element. Note that inverse="true" sides will not be considered bySchemaExport.
|
sql-type
|
SQL column type
|
overrides the default column type (attribute of <column>element only)
|
default
|
SQL expression
|
specify a default value for the column
|
check
|
SQL expression
|
create an SQL check constraint on either column or table
|
The <comment> element allows you to specify comments for the generated schema.
<comment> 要素で生成するスキーマにコメントを指定することができます。
<class name="Customer" table="CurCust">
<comment>Current customers only</comment>
...
</class>
<property name="balance">
<column name="bal">
<comment>Balance in USD</comment>
</column>
</property>
This results in a comment on table or comment on column statement in the generated DDL (where supported).
これにより、生成したDDLに comment on table や comment on column 文が書かれます。
20.1.2. Running the tool(ツールの実行)
The SchemaExport tool writes a DDL script to standard out and/or executes the DDL statements.
SchemaExport は標準出力に対してDDLスクリプトを書き出し、DDL文を実行したりもします。
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
Table 20.2. SchemaExport Command Line Options(SchemaExportのコマンドラインオプション)
Option(オプション)
|
Description(説明)
|
--quiet
|
don't output the script to stdout
|
--drop
|
only drop the tables
|
--create
|
only create the tables
|
--text
|
don't export to the database
|
--output=my_schema.ddl
|
output the ddl script to a file
|
--naming=eg.MyNamingStrategy
|
select a NamingStrategy
|
--config=hibernate.cfg.xml
|
read Hibernate configuration from an XML file
|
--properties=hibernate.properties
|
read database properties from a file
|
--format
|
format the generated SQL nicely in the script
|
--delimiter=;
|
set an end of line delimiter for the script
|
You may even embed SchemaExport in your application:
アプリケーションに SchemaExport を組み込むこともできます:
Configuration cfg = ....;
new SchemaExport(cfg).create(false, true);
20.1.3. Properties(プロパティ)
Database properties may be specified
次のように、データベースのプロパティを指定することができます。
? as system properties with -D<property>( -D<property>を使って、システムプロパティとして)
? in hibernate.properties(hibernate.propertiesファイル内で)
? in a named properties file with -properties(--properties を使って指定したプロパティファイル内で)
The needed properties are:
必要なプロパティは以下のものです:
Table 20.3. SchemaExport Connection Properties(SchemaExportコネクションプロパティ)
Property Name(プロパティ名)
|
Description(説明)
|
hibernate.connection.driver_class
|
jdbc driver class
|
hibernate.connection.url
|
jdbc url
|
hibernate.connection.username
|
database user
|
hibernate.connection.password
|
user password
|
hibernate.dialect
|
dialect
|
20.1.4. Using Ant(Antを使用する)
You can call SchemaExport from your Ant build script:
Antのビルドスクリプトから SchemaExportを呼び出すことができます:
<target name="schemaexport">
<taskdef name="schemaexport"
classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"
classpathref="class.path"/>
<schemaexport
properties="hibernate.properties"
quiet="no"
text="no"
drop="no"
delimiter=";"
output="schema-export.sql">
<fileset dir="src">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaexport>
</target>
20.1.5. Incremental schema updates(インクリメンタルなスキーマ更新)
The SchemaUpdate tool will update an existing schema with "incremental" changes. Note that SchemaUpdate depends heavily upon the JDBC metadata API, so it will not work with all JDBC drivers.
SchemaUpdate ツールは既存のスキーマをインクリメンタルに更新します。SchemaUpdate はJDBCのメタデータAPIに強く依存します。そのため、すべてのJDBCドライバでうまくいくとは限らないことに注意してください。
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaUpdate options mapping_files
Table 20.4. SchemaUpdate Command Line Options(SchemaUpdate のコマンドライン・オプション)
Option(オプション)
|
Description(説明)
|
--quiet
|
don't output the script to stdout
|
--text
|
don't export the script to the database
|
--naming=eg.MyNamingStrategy
|
select a NamingStrategy
|
--properties=hibernate.properties
|
read database properties from a file
|
--config=hibernate.cfg.xml
|
specify a .cfg.xml file
|
You may embed SchemaUpdate in your application:
アプリケーションに SchemaUpdate を組み込むことができます:
Configuration cfg = ....;
new SchemaUpdate(cfg).execute(false);
20.1.6. Using Ant for incremental schema updates(インクリメンタルなスキーマ更新に対するAntの使用)
You can call SchemaUpdate from the Ant script:
Antスクリプトから SchemaUpdate を呼び出すことができます:
<target name="schemaupdate">
<taskdef name="schemaupdate"
classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"
classpathref="class.path"/>
<schemaupdate
properties="hibernate.properties"
quiet="no">
<fileset dir="src">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaupdate>
</target>
20.1.7. Schema validation
The SchemaValidator tool will validate that the existing database schema "matches" your mapping documents. Note that SchemaValidator depends heavily upon the JDBC metadata API, so it will not work with all JDBC drivers. This tool is extremely useful for testing.
SchemaValidator ツールは、既存のデータベーススキーマと作成したマッピングドキュメントが"一致する"ことを検証します。 SchemaValidator はJDBCのメタデータAPIに強く依存することに注意してください。そのため、すべてのJDBCドライバーで作動するものではありません。このツールはテスト時に非常に有用です。
java -cp hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaValidator options mapping_files
Table 20.5. SchemaValidator Command Line Options(SchemaValidator のコマンドライン・オプション)
Option(オプション)
|
Description(説明)
|
--naming=eg.MyNamingStrategy
|
select a NamingStrategy
|
--properties=hibernate.properties
|
read database properties from a file
|
--config=hibernate.cfg.xml
|
specify a .cfg.xml file
|
You may embed SchemaValidator in your application:
SchemaValidator をアプリケーションに組み込むことが出来ます:
Configuration cfg = ....;
new SchemaValidator(cfg).validate();
20.1.8. Using Ant for schema validation(スキーマのバリデーションにAntを使用します)
You can call SchemaValidator from the Ant script:
Antスクリプトから SchemaValidator を呼び出せます:
<target name="schemavalidate">
<taskdef name="schemavalidator"
classname="org.hibernate.tool.hbm2ddl.SchemaValidatorTask"
classpathref="class.path"/>
<schemavalidator
properties="hibernate.properties">
<fileset dir="src">
<include name="**/*.hbm.xml"/>
</fileset>
</schemaupdate>
</target>
No comments:
Post a Comment