Thursday, February 4, 2010

Chapter 11. Hibernate and Eclipse: Really Using the Hibernate Tools








Chapter 11. Hibernate and Eclipse: Really Using the
Hibernate Tools


Installing the Hibernate Tools in Eclipse


Creating a Hibernate Console Configuration


More Editing Support


The Hibernate Console Perspective


Code Generation


Mapping Diagrams




11.1. Installing the Hibernate Tools in Eclipse


If you're working with Eclipse as your Java development environment (as
so many of us are these days), you can take far deeper advantage
of the Hibernate Tools than we have been in the rest of the book (where we
just used them to add Hibernate-related capabilities to our Ant
builds).



11.1.1. How do I do that?


Before diving in, make sure you're running a recent
enough version of Eclipse. The current release of the Hibernate
Tools requires at least Eclipse 3.3 and WTP 2.0. So
here's an excuse to upgrade if you've been putting it off.


The easiest way to install the Hibernate Tools into Eclipse is
through Eclipse's normal update site mechanism.
Start by telling Eclipse where to find Hibernate Tools, by choosing the
Find and Install option in
the Software Update menu, as shown in
Figure 11-1.



Figure 11-1. Updating features in Eclipse








We want to install something completely new, rather than update an
existing plug-in, so choose Search for new
features to install
, and click Next.


Eclipse doesn't automatically know about the Hibernate Tools
update site, so we need to explain where to find it. Click on New Remote Site, as shown in Figure 11-2.



Figure 11-2. Installing from a new Update Site in Eclipse








Enter something descriptive like "Hibernate Tools" for the name of
the new update site. As you'll see later in Figure 11-4, Eclipse
now wants us to specify the URL at which the update
site can be found. We'll have to do a little hunting to figure that
out...


Switch to a web browser, open up the Hibernate web site at http://hibernate.org, and click on Hibernate Tools in the navigation menu on
the lefthand side. (Or you can cut to the chase by opening http://tools.hibernate.org directly.) Once you're there,
you can read a bit about the tools, and follow various documentation
links. We did that for a while, trying to figure out how to install the
tools directly into Eclipse, and eventually noticed what we'd been
missing: there's an update site link right on the Tools page. Copy that
URL to your clipboard, as shown in Figure 11-3 (you can
right-click or Control-click on the
link to get the menu to appear).



Figure 11-3. Getting the Hibernate Tools Update Site URL








If you're paying close attention, you may have noticed that
the version of the Hibernate Tools mentioned on this page is
newer than the beta 9 we've been using elsewhere in the book.
Congratulations—you've spotted an example of how difficult it is to
write about things that change as quickly as large open-source projects!
At the time of this writing, the stable release is too new to be
available through the Maven repository, so we're leaving the rest of the
book alone. The changes in the new release don't affect the other
chapters, in any case, but the latest version is
necessary to work with the current release of Eclipse (3.3, also known
as Europa, at the moment) and to enable some of the nicer features
you'll see in this chapter.


So, paste the URL for the update site into the New Update Site window in Eclipse. You should
see something like Figure 11-4. Click OK to enable the new site.



Figure 11-4. Configuring a new Update Site in Eclipse








As you can see from Figure 11-5, Eclipse makes the reasonable assumption
that you want to use the update site you just configured, so click
Finish to contact that site and
see what can be installed from there.



Figure 11-5. New Update Site is ready to use








The window that pops up (Figure 11-6) is simple and promising enough. All we
need to do is check the single checkbox next to Hibernate Tools, and
install away, right?



Figure 11-6. Installing the Hibernate Tools: we need to dig deeper








Unfortunately, as soon as we check the Hibernate Tools checkbox,
we run into a snag. There's an error message displayed at the top of the
window, and the Next button is
grayed out and unavailable to us.




11.1.2. What went wrong?


Eclipse won't let us proceed, because the SeamTools feature (one
of the 10 plug-ins that make up the Hibernate Tools group) requires
another plug-in,
org.eclipse.datatools.connectivity.feature,
which is not installed by default. Unfortunately, we need to backtrack a
bit. Click Cancel, then start
over with the Find and
Install
menu option as you did at the beginning of this
section. Once again, choose Search for new
features to install
, and click Next. This time, also enable the Europa
Discovery Site, as shown in Figure 11-7, before clicking Finish.



Figure 11-7. Enabling the Eclipse Discovery Site








Pick a mirror appropriate to your location from the list of
choices and click OK (unless
you've configured Eclipse to pick one automatically, which will save you
this step). This time you'll see two groups of plug-ins available, the
Europa Discovery Site and the Hibernate Tools. Once again, choose the
Hibernate Tools group for installation. As before, you'll see an error
at the top of the window, but this time we have the opportunity to fix
that. One option would be to select the entire Europa Discovery Site
group, but that would install a great deal more than is actually
required, adding a great deal of unnecessary bloat to your Eclipse
configuration.


So we want to figure out the minimal set of plug-ins we need from
the Europa Discovery Site. Leaving the Hibernate Tools plug-in selected,
expand the Europa Discovery Site group, but don't try to select
anything. Although it is possible to manually find and select the
plug-in which is being reported as a requirement for the Hibernate
Tools, this leads to a lot of tedious research and trial and error to
figure out exactly which combination of plug-ins is required to get
things working (we tried it, and once we found the plug-in that provided
data tools connectivity feature mentioned in the first error, we found
it had dependencies of its own, and wasn't the only thing missing in the
first place). Just as we were gritting our teeth in preparation for
trying to write up a comprehensible explanation of how to select exactly
what you need, we noticed the Select the
Required
button on the right. You can use it to
automatically select the plug-ins you need. But our experimentation
revealed that it only works if you first expand the disclosure triangle
corresponding to the site from which the required plug-ins will be
downloaded. It did nothing at all until we expanded the Europa Discovery
site. Once that was done, clicking Select
Required
automatically selected just the plug-ins required
to resolve the dependency errors. So, do that now, since you've got the
Hibernate Tools selected and the Europa Discovery site expanded. You
will end up with something like Figure 11-8,
although the exact number of things to be installed will depend on what
you might have already installed yourself in the past.



Figure 11-8. Required plug-ins selected








The important point is that we no longer have an error message
blocking our ability to install the Hibernate Tools! So click Next to install this set of plug-ins. Accept
the licenses that appear in the following dialog and click Next again. Another dialog window pops up
showing you that there are a related set of optional features that you
can also install. Since we don't need them to work with the Hibernate
Tools, just click Next in the
Optional Features dialog to
continue installing the precise set we requested.


The default installation locations (which will vary based on where
Eclipse lives on your machine) are almost always fine, so unless you
have special reasons to put them elsewhere, click Finish in the Installation dialog to go ahead and install
the plug-ins.


The Update Manager will run
for a while as the updates are downloaded. Eventually, a Feature Verification dialog will pop up. For
each feature being installed, this will report whether or not it seems
to be cryptographically signed by an organization that is trusted by the
Eclipse project. Some of them will be; others (like the Hibernate Tools
themselves) will lack valid signatures. This is common for third-party
Eclipse plug-ins. Eclipse presents this situation as a warning, and
wants to make sure you're willing to install a plug-in under those
circumstances. Since I have had confidence in the sites from which I've
downloaded them, I've always proceeded with the installation, anyway. If
you feel the same way (and, basically, if you want to use the Hibernate
Tools, you'll have to), click Install to proceed. (If you don't want to be
asked for each plug-in, click Install
All
to accept them in bulk). The Update Manager will proceed
with the installation. When it finishes with everything, it will suggest
that you restart Eclipse. Go ahead and click Yes, to be safe, and wait for Eclipse to
come back.




11.1.3. Now what?


It's immediately clear that Eclipse knows more about Hibernate than it used to. The icon for the Hibernate
configuration file now gets a little Hibernate logo as part of it, and
double-clicking on it or a mapping file opens a special editor that
makes it easier to create and update such files, without seeing the
underlying XML (Figure 11-9).



Figure 11-9. Hibernate configuration editor








NOTE


This will certainly save me many trips to the reference
documentation whether or not I'm working in source view!



Of course, you can still view the XML if
you're productive that way, by clicking on the Source tab at the bottom of the editor—and
you'll find that you now get completion assistance for the various
Hibernate-related elements and their values when
working with the source, as shown in Figure 11-10.
This goes beyond the XML editor's normal completion
abilities for element names, which it can perform by analyzing the
XML DTD, since the
DTD says nothing about property names other than that
they're text.



Figure 11-10. Autocompletion of property names in the Hibernate configuration
editor









You use the normal Eclipse completion key sequence, ControlSpace, to bring up these pop ups.




The editor for mapping documents is shown in Figure 11-11. These
two editors seem quite powerful and useful; it's worth
playing around with them for a while to get a feel for how they work and
what they can do. By themselves they'd make a worthwhile addition to
Eclipse for working with Hibernate.



Figure 11-11. Hibernate mapping editor








To explore some of the other things you can do with the Hibernate
Tools, they need to be activated for a project. If you're starting a new
project, you can choose to create a new Hibernate configuration file. If
you've got an existing Hibernate project, as we did working up this
chapter, you can skip that part and simply create a new
Hibernate console.










No comments: