advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Reviews : Java Books : Hibernate: A Developer's Notebook :

Buy this book
Title: Hibernate: A Developer's Notebook
ISBN: 0596006969
US Price: $16.97
©2004 O'Reilly

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.

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

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs