Generating the Database Schema
Despite my first impression that everything flowed from the mapping document, Hibernate Synchronizer doesn't currently offer any support for creating or updating a database schema from your mapping documents. There has already been a request posted to the support forum about this, and it wouldn't surprise me if we saw these features in the future; support shouldn't be too difficult. For now, you'll have to stick with an approach like the Ant-driven one in Hibernate: A Developer's Notebook if you're developing your database from your mappings. Alternately, the Hibernator plugin described below does support schema updates from within Eclipse. I may have to investigate whether it's possible to have both of these plugins installed at the same time.
Well, I certainly hope this whirlwind tour has given you an sense of the capabilities offered by the plugin. I haven't covered all of them, by any means, so do download it and explore on your own if anything has intrigued you.
Clearly you can do some neat things with Hibernate Synchronizer. Will I be using it for my own Hibernate projects? There are some pluses and minuses to that idea, and I probably won't decide until I get to the point of actually adopting Hibernate in place of our homebrew (and very simplistic) lightweight O/R tool at work. That is going to be a significant enough change that we are putting it off until we tackle a major architecture shift that's on the horizon for other reasons. Here are some of the factors that will weigh in my decision.
As mentioned in the Installation section, there is a little bit of concern about the license out there. The plugin's forum has a discussion about this. The current license is based on a custom modification of the GNU GPL that removes all the source-sharing provisions, but tries to retain the other aspects of "copyleft" protection. There is some question about the legitimacy of this, and the author is looking for an alternative. It is clear that the intention is to protect the plugin, not to encumber any other project that happens to use the plugin to generate code, but it may be worth carefully reading the current license to see if you believe that intent has been achieved, or if there is too much risk for you.
The same discussion reveals that the author had originally released the plugin as open source, but withdrew it temporarily because he felt it wasn't yet polished enough to serve as a good example to others. He then had some very annoying email interactions with hotheads who, sadly, soured him on the whole idea of sharing the source. It is certainly his prerogative to decide what, if anything, to share with us. The plugin is a gift to the world, and the author doesn't owe us anything. But I hope that enough positive interactions with other users might help convince him to go back to his original plan of sharing the source. I really value having access to the source code of tools that I use, not only because it is a very valuable learning opportunity, but because it means I (or others) can fix little problems immediately if we need to. The author has been very responsive so far in addressing user concerns, but no one person can keep up as well as a community, and we all sometimes get busy, burned out, or otherwise distracted.
The fact that Hibernate Synchronizer uses its own templates and mechanism to generate your data access class is both positive and negative. It's positive in that it gives you more capabilities than Hibernate's "standard" code generation tools. The ability to work with an auto-generated subclass of your data object in which you can embed business logic without fear of it getting overwritten when you regenerate the access code is a big plus. And there are other niceties offered by the plugin's generated classes that make many of the simple cases even simpler.
On the other hand, this also means that Hibernate Synchronizer's generated code can lag behind Hibernate when there are new features added or changes made to the platform. The plugin's code is also more likely to have bugs in its support for Hibernate's less-used modes: it has a much smaller user base, and a single person keeping it updated. You can see evidence of this phenomenon on the discussion forum.
As with so many things, it's up to you to decide whether the potential benefits outweigh the risks. Even if you don't use the code generator, you might find the mapping editor extremely useful. You can turn off automatic synchronization if you want to just use the editor's completion and assistance features.
If you do adopt the plugin and find it useful, I would definitely encourage you to contact the author and thank him, and consider donating some money to help support its further development.
In my hunting so far, I've encountered two more plugins that offer support for Hibernate within Eclipse. (If you know of others, or come across them in the future, I'd be interested in learning about them.) Perhaps I'll write articles about these in the future.
The HiberClipse plugin looks like another very useful tool. It seems geared towards a database-driven workflow, where you've already got a database schema and want to build a Hibernate mapping file and Java classes to work with it. This is a common scenario, and if you find yourself facing such a challenge, I'd definitely recommend checking out this plugin. One really cool feature it offers is a graphical "relationships view" of the database you're working with, right within Eclipse. (I should point out that Hibernate Synchronizer doesn't leave you high and dry if you want to start with an existing database schema, either. Its New Mapping File Wizard can connect to your database and build the mapping file based on what it finds.)
Figure 28. Hibernate Synchronizer's Mapping Wizard
Finally, Hibernator seems to lean in the opposite direction, starting from your Java code to generate a simple Hibernate mapping document, and then from there letting you build (or update) the database schema. It also offers the ability to run database queries within Eclipse. Of the three plugins, it appears to be at the earliest stages of development, but already looks worth keeping an eye on, especially since it cites members of the Hibernate development team as contributors.
If I've managed to pique your interest in this article, there are plenty of resources to help you dig deeper into these topics. In addition to the sites I've linked to throughout the text, there are some books that might interest you. Of course, I have to mention my own, Hibernate: A Developer's Notebook. For in-depth reference material about Hibernate, the online documentation is very useful, especially the reference manual, and there is a forthcoming book by the developers of Hibernate itself, Hibernate in Action. I look forward to reading that myself.
As for Eclipse, I'm currently working through Steve Holzner's Eclipse and looking forward to the Eclipse Cookbook that will be released later this month. My blog discusses my Eclipse "conversion" in more detail in case you're curious about that (or teetering on the edge yourself). If you're just getting started, be sure to explore the
"Getting Started" sections of Eclipse's built-in Workbench and Java Development
user guides. These show you how the environment is intended to be used, give you
some good suggestions, and walk you through processes and features you might not
otherwise discover quickly on your own. Choose
Help Contents within Eclipse to find them.
In May 2004, O'Reilly Media, Inc., released Hibernate: A Developer's Notebook.
Sample Chapter 3, "Harnessing Hibernate," is available free online.
For more information, or to order the book, click here.