How to use XDoclet and Ant to Generate Hibernate Mapping and Configuration

XDoclet is a wonderful tool to generate various kind of configuration files in declarative way, and the declaration can be done with java code. It allows Java Developers to work in Java Code files only even for all the configuration which otherwise need to be defined in XML or property files. It is very convenient way for Developers who like to have everything in Code files, and facilitate to do all configuration from same place. It makes the applications easy to maintain, and debug. XDoclet supports tags for various tools like Hibernate, EJB, JMX, JSF, and Spring etc. However here we are explaining how to use it with Hibernate. XDoclet can be used for various purposes with Hibernate, like, to generate Hibernate Mapping Files, to generate Hibernate Configuration File, and to Generate/Update the schema.

Before going into details, you should be aware that there are two active versions of XDoclet, version 1 and version 2. Difficulty is that these two versions do not seem to be in Sync. Like version 2 has many changes which are not compatible with version 1 and also has a lot of changes in API. Another problem is that it is not easy to find a good documentation for XDoclet 2, and details like the difference between version 1 and 2. We were unable to find any single document which can provide all of these information nicely, and the path to update from version 1 to 2 with all side effects and changes required. There are many changes in XDoclet tags for Hibernate in version 1 and 2. Many of old tags are not supported now. Same difference is there in ANT tasks also, which can be used to integrate the XDoclet features with ANT. Here we are taking a real life scenario for using XDoclet task in ANT for Hibernate.

Suppose, there are multiple projects dependent on one or other project for Data Objects. One project has all Global Data objects like Address, City, Country etc. Other project has some business function specific data like Employee, Supplier etc. Now Employee has a field of Address type also, which will of course be coming from Global Data Project.


Now to resolve the java dependency, we can make a jar of all data objects from Global project and refer it from other project. This works well for java compilation. Now the next step is to generate the HBM and configuration file and further to generate the database schema using XDoclet. Here HBM files can be generated using XDoclet ant task, and similarly configuration file can also be generated. But when you will try to generate the database schema, it will give you the error that Address class is not mapped. It is because database schema generation tool is finding the dependency of Employee on Address, however it is unable to find the HBM file for Address with configuration. So we need a way using which we can add dependent HBM files to Configuration file, so that schema generation tool can find these. Now we are using XDoclet 2, being more advance in some of the features. But the problem comes in shape that XDoclet 2 ant task does not provide any facility to refer the external dependencies, i.e. other HBM files. At least, we didn't find any solution for it. If it provides this feature, this documentation is not easily reachable on internet. After a lot of exploration on internet, in documents and in source code, we didn't find any good solution. XDoclet1 provides this functionality, but we can not use it due to other enhanced features in XDoclet2.

Finally, we decided to merge the two versions of XDoclet i.e. we have used XDoclet 2 to generate the HBM mapping files, so that it can support the latest features. And then we used the XDoclet 1 ant task to generate the Hibernate Config file, which provides the facility to include dependencies using "otherMapping" tag with it. Finally to generate the schema, we have used Hibernate Tools ant task, which we found more suitable than other options. So we merge three different tools to achieve our goal. Here are the ANT targets examples for all of these three steps.

Ant Target to generate the HBM files from Java files using XDoclet2:-






















Ant target to generate the Hibernate Configuration file using XDoclet1:-


























Ant target to Generate the Schema:-


























By following above mentioned steps, and by mixing the various versions of XDoclet and Hibernate Tools, you should be able to generate the configuration for Hibernate. Make sure that HBM files for dependency classes should be available at same path where other HBM files are stored for this functional project. It is not a straight path to achieve the final goal, and there can be many other better ways. But it is one of the way, which we found working for us. Enjoy XDoclet and Hibernate!!



People who read this post also read :



1 comments:

Mohit Gupta said...

Link for Documentation of Hibernate Tags with XDoclet2: http://xdoclet.codehaus.org/HibernateTags

Link for XDoclet1: http://xdoclet.sourceforge.net/xdoclet/index.html

Post a Comment