Tuesday, August 6, 2013

XML based mapping VS Annotation based mapping in hibernate

There are two ways that we can perform entity mapping in hibernate.
1. XML based mapping
2. Annotation based mapping.

the intention of this article is to select the appropriate mapping option for your project. by the way, i always prefer the annotation based mapping for my projects because of the following reasons.

  • i personally hate a load of XML mapping files and it adds unnecessary complexity for the project. 
  • it degrade the readability and understandability of the code because your POJO (Plain Old Java Objects) is independent from the relevant XML mapping files. therefore you cannot understand the database constraints, associations and mapping relationships between entities just looking at only the POJOs. you need an additional effort to read the XML mapping files and POJOs simultaneously to get an understanding of how entity mapping has been done.

 i personally prefer the annotation based mapping because of the following reasons.

  •  it is very simple to perform entity mapping with annotations and i don't have to maintain separate XML configuration files. 
  • In Annotation mapping,the entity relationships, mappings and other DB constraints are also defined in the POJO along with the relevant attribute. therefore it improves the understandability and the readability of the code.

The decision of selecting the appropriate mapping option (either Annotation based or XML based) for your project should be done wisely.

when to use XML based mapping


it is safer to use XML based mapping to integrate/use the hibernate for a project that is already developed. since you are using XML based mapping, you do not have to touch or modify the POJO classes that are already developed and stable. if you are confident enough to modify the POJOs, then you may use annotation based mapping.(But not recommended)

Another scenario where we can use the XML based mapping is that, we are getting a request  to integrate/use the hibernate for a project that is already developed. but the problem is we do not have the source code of the POJO classes. at that time, we cannot use the annotation to map the entities because, we do not have the source code of POJOs. in such case, we have to use the XML based mapping.


when to use the Annotation based mapping

XML based mapping is a proprietary format of Hibernate ORM framework.
if you intend to expose your applications to other JPA2 compliant other ORM frameworks, you must use the Annotation based mapping. but keep it in mid that there are two types of annotations.
  •  Annotations in the javax.persistance package
In oder to comply, your application with the JPA 2 specification standards, you must use the annotations provided in this package.
  •  Annotations in the org.hibernate package
Annotations provided in this package also specific to the hibernate. In otherwords, all these annotations are owned and governed by the hibernate. therefore  if you have used this annotations,your application will not comply with JPA 2. therefore the application will not work with other ORM frameworks compliant with JPA2. therefore using these annotations will produce the same effect as XML based mapping. only difference between the XML and Annotation (org.hibernate package) based mapping is that it is XML and this is annotations.



Hope this will be helpful for you!
Cheers
Chathuranga Tennakoon
www.chathurangaonline.com

No comments:

Post a Comment