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

What just happened?

We took the abstract description of the information about music tracks that we wanted to represent in our Java code and database, and turned it into a rigorous specification in the format that Hibernate can read. Hopefully you'll agree that it's a pretty compact and readable representation of the information. Next we'll look at what Hibernate can actually do with it.

What about...

. . . Other data types, including nested classes and enumerations? Relationships between tables? Indices? Class hierarchies and polymorphism? Tables that contain rows we need to ignore? Hibernate can handle all these things, and we'll cover most of them in later examples. Appendix A lists the basic types that Hibernate supports "out of the box."

Generating Some Class

Our mapping contains information about both the database and the Java class between which it maps. We can use it to help us create both. Let's look at the class first.

How do I do that?

The Hibernate Extensions you installed in Chapter 1 included a tool that can write Java source matching the specifications in a mapping document, and an Ant task that makes it easy to invoke from within an Ant build file. Edit build.xml to add the portions shown in bold in Example 2-2.

Example 2-2. The Ant build file updated for code generation

1    <project name="Harnessing Hibernate: The Developer's Notebook" 
2    
3             default="db" basedir=".">
4      <!-- Set up properties containing important project directories -->
5      <property name="source.root" value="src"/>
6      <property name="class.root" value="classes"/>
7      <property name="lib.dir" value="lib"/>
8      <property name="data.dir" value="data"/>
9    
10     <!-- Set up the class path for compilation and execution -->
11     <path id="project.class.path">
12         <!-- Include our own classes, of course -->
13         <pathelement location="${class.root}" />
14         <!-- Include jars in the project library directory -->
15         <fileset dir="${lib.dir}">
16           <include name="*.jar"/>
17         </fileset>
18     </path>
19   
20     <target name="db" description="Runs HSQLDB database management UI
21   against the database file--use when application is not running">
22         <java classname="org.hsqldb.util.DatabaseManager"
23               fork="yes">
24            <classpath refid="project.class.path"/>
25            <arg value="-driver"/>
26            <arg value="org.hsqldb.jdbcDriver"/>
27            <arg value="-url"/>
28            <arg value="jdbc:hsqldb:${data.dir}/music"/>
29            <arg value="-user"/>
30            <arg value="sa"/>
31         </java>
32     </target>
33   
34     <!— Teach Ant how to use Hibernate's code generation tool —>
35     <taskdef name="hbm2java"
36              classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
37              classpathref="project.class.path"/>
38   
39     <!— Generate the java code for all mapping files in our source tree —>
40     <target name="codegen"
41             description="Generate Java source from the O/R mapping files">
42       <hbm2java output="${source.root}">
43         <fileset dir="${source.root}">
44           <include name="**/*.hbm.xml"/>
45         </fileset>
46       </hbm2java>
47     </target>
48   
49   </project>

We added a taskdef (task definition) and a new target to the build file. The task definition at line 33 teaches Ant a new trick: it tells Ant how to use the hbm2java tool that is part of the Hibernate Extensions, with the help of a class provided for this purpose. Note that it also specifies the class path to be used when invoking this tool, using the project.class.path definition found earlier in the file.

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.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
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%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs