Dependency injection of @Entity annotated POJOs has greatly simplified the configuration of applications that use an ORM framework like Hibernate or JPA, but that doesn't mean that using annotations is the answer to every problem. In fact, there are many cases when using annotations is a bad approach, and it is much preferable to use a basic, XML configuration file with individual classes being mapped explicitly to the ORM entity manager. Here are four scenarios where an enterprise architect might want to pass on annotations, and use an XML configuration file instead:
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
Annotations are not supported by your application
This is an obvious point, but there are still many legacy applications that have not moved to an environment that supports annotations. If you are looking at supporting older applications, you simply might not have the option to use annotations available to you, which means using an XML based configuration file all the way.
Important to externalize information from the application
The great thing about using XML for configuration is the fact that persistence information is externalized from your POJO. A great deal of effort is put into creating loosely coupled applications, yet developers are eager to put persistence specific annotations into model components that are supposed to be detached from the database. Using XML for configuration helps to improve the de-coupling of application layers.
Large number of entities in different packages
If the application in question contains a large number of entities, which happen to be in a variety of different packages, finding and editing POJOs when changes need to be made can be challenging. Having a single XML file that lists all of the entities makes editing ORM settings much easier, as opposed to having to sift through various folders and packages.
Potential for regular updates
If the JPA configuration is going to be updated frequently, doing this in one, single, XML file makes the process much easier and manageable. Performing simple find and replace type of operations are very easy in a single configuration file, but unwieldily when attempted on multiple Java files.
Annotations are minimally verbose and easily readable to developers, which is why developers love them. Furthermore, there is the fact that errors in annotations can be picked up at development time by the compiler, whereas similar errors in XML files will only be picked up at runtime, which often involves a time consuming deployment. Annotations are indeed becoming the most popular way to integrate ORM frameworks into new applications, but developers need to be aware of the fact that there are indeed circumstances where using an XML configuration file is the better option.
What rules do you have about when to use an XML file over annotations? Let us know.