Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lect17-hibernate-orm / more / hibernate_reference.pdf
Скачиваний:
63
Добавлен:
18.03.2015
Размер:
2.2 Mб
Скачать

 

Properties

 

 

Option

Description

 

 

--text

do not 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

 

 

--

read database properties from a file

properties=hibernate.properties

 

 

 

--format

format the generated SQL nicely in the script

 

 

--delimiter=;

set an end of line delimiter for the script

 

 

You can even embed SchemaExport in your application:

Configuration cfg = ....

;

new SchemaExport(cfg).create(false, true);

22.1.3. Properties

Database properties can be specified:

as system properties with -D<property>

in hibernate.properties

in a named properties file with --properties

The needed properties are:

Table 22.3. SchemaExport Connection Properties

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

 

 

22.1.4. Using Ant

You can call SchemaExport from your Ant build script:

<target name="schemaexport">

<taskdef name="schemaexport"

classname="org.hibernate.tool.hbm2ddl.SchemaExportTask"

351

Chapter 22. Toolset Guide

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>

22.1.5. Incremental schema updates

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

Table 22.4. SchemaUpdate Command Line Options

Option

Description

 

 

--quiet

do not output the script to stdout

 

 

--text

do not export the script to the database

 

 

--naming=eg.MyNamingStrategy

select a NamingStrategy

 

 

--

read database properties from a file

properties=hibernate.properties

 

 

 

--config=hibernate.cfg.xml

specify a .cfg.xml file

 

 

You can embed SchemaUpdate in your application:

Configuration cfg = ....;

new SchemaUpdate(cfg).execute(false);

22.1.6. Using Ant for incremental schema updates

You can call SchemaUpdate from the Ant script:

<target name="schemaupdate">

<taskdef name="schemaupdate"

classname="org.hibernate.tool.hbm2ddl.SchemaUpdateTask"

352

Schema validation

classpathref="class.path"/>

<schemaupdate

properties="hibernate.properties"

quiet="no">

<fileset dir="src">

<include name="**/*.hbm.xml"/>

</fileset> </schemaupdate>

</target>

22.1.7. Schema validation

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:

Table 22.5. SchemaValidator Command Line Options

Option

Description

 

 

--naming=eg.MyNamingStrategy

select a NamingStrategy

 

 

--

read database properties from a file

properties=hibernate.properties

 

 

 

--config=hibernate.cfg.xml

specify a .cfg.xml file

 

 

You can embed SchemaValidator in your application:

Configuration cfg = ....

;

new SchemaValidator(cfg).validate();

22.1.8. Using Ant for schema validation

You can call SchemaValidator from the Ant script:

<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"/>

353

Chapter 22. Toolset Guide

</fileset>

</schemavalidator>

</target>

354

Соседние файлы в папке more