Hibernate.orgCommunity Documentation
Roundtrip engineering with Hibernate is possible using a set of Eclipse plugins, commandline tools, and Ant tasks.
Hibernate Tools currently include plugins for the Eclipse IDE as well as Ant tasks for reverse engineering of existing databases:
Mapping Editor: an editor for Hibernate XML mapping files that supports auto-completion and syntax highlighting. It also supports semantic auto-completion for class names and property/field names, making it more versatile than a normal XML editor.
Console: the console is a new view in Eclipse. In addition to a tree overview of your console configurations, you are also provided with 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.
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 to reverse engineer an existing database schema into POJO source files and Hibernate mapping files. The reverse engineering wizard supports customizable templates.
Please refer to the Hibernate Tools package documentation for more information.
However, the Hibernate main package comes bundled with an integrated tool : SchemaExport aka hbm2ddl
.It can even be used from "inside" Hibernate.
DDL can 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.
You must specify a SQL Dialect
via the hibernate.dialect
property when using this tool, as DDL is highly vendor-specific.
First, you must customize your mapping files to improve the generated schema. The next section covers schema customization.
Many Hibernate mapping elements define optional attributes named length
, precision
and scale
. You can set the length, precision and scale of a column with this attribute.
<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.
<many-to-one name="bar" column="barId" not-null="true"/>
<element column="serialNumber" type="long" not-null="true" unique="true"/>
A unique-key
attribute can 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. It is only used to group the columns in the mapping file.
<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 can be grouped into the same index by simply specifying the same index name.
<property name="lastName" index="CustName"/>
<property name="firstName" index="CustName"/>
A foreign-key
attribute can be used to override the name of any generated foreign key constraint.
<many-to-one name="bar" column="barId" foreign-key="FKFooBar"/>
Zahlreiche Mapping-Elemente akzeptieren auch ein untergeordnetes <column>
-Element. Das ist insbesondere für das Mappen von vielspaltigen Typen hilfreich:
<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 allows you to 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.
<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
>
Das sql-type
-Attribut ermöglicht die Außerkraftsetzung des Standard-Mappings eines Hibernate-Typs zum SQL-Datentyp.
<property name="balance" type="float">
<column name="balance" sql-type="decimal(13,3)"/>
</property
>
Das check
-Attribut ermöglicht es Ihnen, eine Prüfungsbedingung festzulegen.
<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
>
The following table summarizes these optional attributes.
Tabelle 21.1. Zusammenfassung
Attribut | Werte | Bedeutung |
---|---|---|
length | Zahl | Spaltenlänge |
precision | Zahl | Dezimale Genauigkeit der Spalte |
scale | Zahl | Dezimale Skalierung der Spalte |
not-null | true|false | specifies that the column should be non-nullable |
unique | true|false | bestimmt, dass die Spalte eine eindeutige Bedingung besitzt |
index | index_name | bestimmt den Namen eines (mehrspaltigen) Index |
unique-key | unique_key_name | bestimmt den Namen einer mehrspaltigen, eindeutigen Bedingung |
foreign-key | foreign_key_name | legt den Namen einer Bedingung des Fremdschlüssels fest, der für eine Assoziation generiert wurde für ein <one-to-one> , <many-to-one> , <key> oder <many-to-many> -Mapping-Element. Bitte beachten Sie, dass inverse="true" -Seiten beim SchemaExport nicht berücksichtigt werden. |
sql-type | SQL column type | setzt den Standard-Spaltentyp außer Kraft (nur Attribut von <column> -Element) |
default | SQL-Ausdruck | bestimmt einen Standardwert für die Spalte |
check | SQL-Ausdruck | erstellt eine SQL-Überprüfungsbedingung an entweder der Spalte oder der Tabelle |
Das <comment>
-Element erlaubt die Bestimmung von Kommentaren für das generierte Schema.
<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.
Das SchemaExport
-Tool schreibt ein DDL-Skript um DDL-Anweisungen zu standardisieren und/oder auszuführen.
The following table displays the SchemaExport
command line options
java -cp
hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaExport
options mapping_files
Tabelle 21.2. SchemaExport
-Befehlszeilenoptionen
Option | Beschreibung |
---|---|
--quiet | do not output the script to stdout |
--drop | nur Tabellen droppen |
--create | nur Tabellen erstellen |
--text | do not export to the database |
--output=my_schema.ddl | ddl-Skript an eine Datei ausgeben |
--naming=eg.MyNamingStrategy | wählen Sie eine NamingStrategy |
--config=hibernate.cfg.xml | Hibernate Konfiguration aus einer XML-Datei lesen |
--properties=hibernate.properties | Datenbank-Properties aus einer Datei lesen |
--format | generierte SQL sauber im Skript formatieren |
--delimiter=; | einen Delimiter für das Zeilenende des Skripts setzen |
You can even embed SchemaExport
in your application:
Configuration cfg = ....;
new SchemaExport(cfg).create(false, true);
Database properties can be specified:
wie System-Properties mit -D
<property>
in hibernate.properties
in einer benannten Properties-Datei mit --properties
Die benötigten Properties sind:
Tabelle 21.3. SchemaExport-Connection-Properties
Property-Name | Beschreibung |
---|---|
hibernate.connection.driver_class | jdbc-Treiberklasse |
hibernate.connection.url | jdbc url |
hibernate.connection.username | Datenbankbenutzer |
hibernate.connection.password | Benutzer-Passwort |
hibernate.dialect | Dialekt |
Sie können SchemaExport
vom Ihrem Ant-Build-Skript aufrufen:
<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
>
The SchemaUpdate
tool will update an existing schema with "incremental" changes. The SchemaUpdate
depends upon the JDBC metadata API and, as such, will not work with all JDBC drivers.
java -cp
hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaUpdate
options mapping_files
Tabelle 21.4. SchemaUpdate
-Befehlszeilenoptionen
Option | Beschreibung |
---|---|
--quiet | do not output the script to stdout |
--text | do not export the script to the database |
--naming=eg.MyNamingStrategy | wählen Sie eine NamingStrategy |
--properties=hibernate.properties | Datenbank-Properties aus einer Datei lesen |
--config=hibernate.cfg.xml | eine .cfg.xml -Datei bestimmen |
You can embed SchemaUpdate
in your application:
Configuration cfg = ....;
new SchemaUpdate(cfg).execute(false);
Sie können SchemaUpdate
vom Ant-Skript aufrufen:
<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
>
The SchemaValidator
tool will validate that the existing database schema "matches" your mapping documents. The SchemaValidator
depends heavily upon the JDBC metadata API and, as such, will not work with all JDBC drivers. This tool is extremely useful for testing.
java -cp
hibernate_classpaths org.hibernate.tool.hbm2ddl.SchemaValidator
options mapping_files
The following table displays the SchemaValidator
command line options:
Tabelle 21.5. SchemaValidator
-Befehlszeilenoptionen
Option | Beschreibung |
---|---|
--naming=eg.MyNamingStrategy | wählen Sie eine NamingStrategy |
--properties=hibernate.properties | Datenbank-Properties aus einer Datei lesen |
--config=hibernate.cfg.xml | eine .cfg.xml -Datei bestimmen |
You can embed SchemaValidator
in your application:
Configuration cfg = ....;
new SchemaValidator(cfg).validate();
Sie können SchemaValidator
vom Ant-Skript aus aufrufen:
<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>
</schemavalidator>
</target
>
Copyright © 2004 Red Hat, Inc.