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.