|
WARNING
You might think the drop="no" setting in our
schematask means you can use
it to update the schema—it won't drop the
tables, right? Alas, this is a misleading parameter name: it means it
won't just drop the tables,
rather it will go ahead and generate the schema
after dropping them. Much as you want to avoid
the codegen task after making any changes to the
generated Java source, you mustn't export the schema
if you've put any data into the database. Luckily,
there is another tool you can use for incremental schema updates that
works much the same way, as long as your JDBC driver is powerful
enough. This SchemaUpdate tool can be used with an
Ant taskdef too.
Because we've asked the schema export task not to be
"quiet," we want it to generate
some log entries for us. In order for that to work, we need to
configure log4j, the logging environment used by
Hibernate. The easiest way to do this is to
make a
log4j.properties file available at the root of
the class path. We can take advantage of our existing
prepare target to copy this from the
src to the classes
directory at the same time it copies Hibernate's
properties. Create a file named log4j.properties
in the src directory with the content shown in
Example 2-6. An easy way to do this is to copy the
file out of the src directory in the Hibernate
distribution you downloaded, since it's provided for
use by their own examples. If you're typing it in
yourself, you can skip the blocks that are commented out; they are
provided to suggest useful logging alternatives.
Example 2-6. The logging configuration file, log4j.properties
1 ### direct log messages to stdout ###
2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3 log4j.appender.stdout.Target=System.out
4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
6
7 ### direct messages to file hibernate.log ###
8 #log4j.appender.file=org.apache.log4j.FileAppender
9 #log4j.appender.file.File=hibernate.log
10 #log4j.appender.file.layout=org.apache.log4j.PatternLayout
11 #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
12
13 ### set log levels - for more verbose logging change 'info' to 'debug' ###
14
15 log4j.rootLogger=warn, stdout
16
17 log4j.logger.net.sf.hibernate=info
18
19 ### log just the SQL
20 #log4j.logger.net.sf.hibernate.SQL=debug
21
22 ### log JDBC bind parameters ###
23 log4j.logger.net.sf.hibernate.type=info
24
25 ### log schema export/update ###
26 log4j.logger.net.sf.hibernate.tool.hbm2ddl=debug
27
28 ### log cache activity ###
29 #log4j.logger.net.sf.hibernate.cache=debug
30
31 ### enable the following line if you want to track down connection ###
32 ### leakages when using DriverManagerConnectionProvider ###
33 #log4j.logger.net.sf.hibernate.connection.DriverManagerConnectionProvider=trace
TIP:
With the log configuration in place, you might want to edit the
codegen target in build.xml
so that it, too, depends on our new prepare
target. This will ensure logging is configured whenever we use it,
preventing the warnings we saw when first running it. As noted in the
tip about class paths and task definitions in the previous section,
though, to make it work the very first time you'll
have to move the taskdef for
hbm2java inside the codegen
target, in the same way we put schemaexport inside
the schema target.
Time to make a schema! From the project directory, execute the
command antschema.
You'll see output similar to Example 2-7 as the classes directory
is created and populated with resources, the Java source is
compiled,
[1]
and the schema generator is run.
Example 2-7. Output from building the schema using HSQLDB's embedded database server
1 % ant schema
2 Buildfile: build.xml
3
4 prepare:
5 [mkdir] Created dir: /Users/jim/Documents/Work/OReilly/Hibernate/Examples/
6 ch02/classes
7 [copy] Copying 3 files to /Users/jim/Documents/Work/OReilly/Hibernate/
8 Examples/ch02/classes
9
10 compile:
11 [javac] Compiling 1 source file to
/Users/jim/Documents/Work/OReilly/Hibernate/Examples/ch02/classes
12
13 schema:
14 [schemaexport] 23:50:36,165 INFO Environment:432 - Hibernate 2.1.1
15 [schemaexport] 23:50:36,202 INFO Environment:466 - loaded properties from
16 resource hibernate.properties: {hibernate.connection.username=sa, hibernate.
17 connection.password=, hibernate.cglib.use_reflection_optimizer=true, hibernate.
18 dialect=net.sf.hibernate.dialect.HSQLDialect, hibernate.connection.url=jdbc:
19 hsqldb:data/music, hibernate.connection.driver_class=org.hsqldb.jdbcDriver}
20 [schemaexport] 23:50:36,310 INFO Environment:481 - using CGLIB reflection
21 optimizer
22 [schemaexport] 23:50:36,384 INFO Configuration:166 - Mapping file: /Users/jim/
23 Documents/Work/OReilly/Hibernate/Examples/ch02/classes/com/oreilly/hh/Track.hbm.
24 xml
25 [schemaexport] 23:50:37,409 INFO Binder:225 - Mapping class: com.oreilly.hh.
26 Track -> TRACK
27 [schemaexport] 23:50:37,928 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.HSQLDialect
28 [schemaexport] 23:50:37,942 INFO Configuration:584 - processing one-to-many association mappings
29 [schemaexport] 23:50:37,947 INFO Configuration:593 - processing one-to-one
30 association property references
31 [schemaexport] 23:50:37,956 INFO Configuration:618 - processing foreign key
32 constraints
33 [schemaexport] 23:50:38,113 INFO Configuration:584 - processing one-to-many
34 association mappings
35 [schemaexport] 23:50:38,124 INFO Configuration:593 - processing one-to-one
36 association property references
37 [schemaexport] 23:50:38,132 INFO Configuration:618 - processing foreign key
38 constraints
39 [schemaexport] 23:50:38,149 INFO SchemaExport:98 - Running hbm2ddl schema export
40 [schemaexport] 23:50:38,154 INFO SchemaExport:117 - exporting generated schema
41 to database
42 [schemaexport] 23:50:38,232 INFO DriverManagerConnectionProvider:41 - Using
43 Hibernate built-in connection pool (not for production use!)
44 [schemaexport] 23:50:38,238 INFO DriverManagerConnectionProvider:42 - Hibernate
45 connection pool size: 20
46 [schemaexport] 23:50:38,278 INFO DriverManagerConnectionProvider:71 - using
47 driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:data/music
48 [schemaexport] 23:50:38,283 INFO DriverManagerConnectionProvider:72 - connection
49 properties: {user=sa, password=}
50 [schemaexport] drop table TRACK if exists
51 [schemaexport] 23:50:39,083 DEBUG SchemaExport:132 - drop table TRACK if exists
52 [schemaexport] create table TRACK (
53 [schemaexport] TRACK_ID INTEGER NOT NULL IDENTITY,
54 [schemaexport] title VARCHAR(255) not null,
55 [schemaexport] filePath VARCHAR(255) not null,
56 [schemaexport] playTime TIME,
57 [schemaexport] added DATE,
58 [schemaexport] volume SMALLINT
59 [schemaexport] )
60 [schemaexport] 23:50:39,113 DEBUG SchemaExport:149 - create table TRACK (
61 [schemaexport] TRACK_ID INTEGER NOT NULL IDENTITY,
62 [schemaexport] title VARCHAR(255) not null,
63 [schemaexport] filePath VARCHAR(255) not null,
64 [schemaexport] playTime TIME,
65 [schemaexport] added DATE,
66 [schemaexport] volume SMALLINT
67 [schemaexport] )
68 [schemaexport] 23:50:39,142 INFO SchemaExport:160 - schema export complete
69 [schemaexport] 23:50:39,178 INFO DriverManagerConnectionProvider:137 - cleaning
70 up connection pool: jdbc:hsqldb:data/music
71
72 BUILD SUCCESSFUL
73 Total time: 10 seconds
Toward the end of the schemaexport section you can
see the actual SQL used by Hibernate to create the
TRACK table. If you look at the start of the
music.script file in the
data directory, you'll see
it's been incorporated into the database. For a
slightly more friendly (and perhaps convincing) way to see it,
execute ant db to fire up the HSQLDB graphical
interface, as shown in Figure 2-1.

Figure 2-1. The database interface with our new TRACK table expanded, and a query
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|