Annotation versus XML configuration: Which JPA option to choose?
By Lukas Stewart
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:
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.
04 Oct 2013
Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.