Hibernate Annotations vs XML Mappings

As we know that prior to the inline annotations, the only way to create hibernate mappings was through XML files. Various tools from Hibernate and third-party projects allowed part or all of these mappings to be generated from Java source code automatically.

Today annotations are the newest way to define mappings but it is not automatically the best way to do so. Let’s discuss the drawbacks and benefits of annotations over XML before discussing when to apply them.

1. Cons of Using Annotations

Let us list down all possible cons one by one.

  • If we are upgrading from a legacy Hibernate environment or working with an existing legacy Hibernate environment, we will already have XML-based mapping files to support our codebase. All else being equal, we will not want to re-express these mappings using annotations just for the sake of it. We would like to stick with mappings because they are still functional and working well. Remember, don’t fix it if it isn’t broken.
  • So, If we are migrating from a legacy environment, we may not want to alter the pre-existing POJO source code, in other words, we will not like to change the known good code with possible bugs.
  • If we do not have the source code to our POJOs (because it was generated by an automated tool or something similar e.g. legacy code), we may prefer the use of external XML-based mappings in comparison to the decompilation of class files to obtain Java source code for alteration.
  • Maintaining the mapping information as external XML files allows that mapping information to be modified to reflect business changes or schema alterations without forcing us to rebuild the application as a whole.

2. Pros of Using Annotations

  • Having considered the drawbacks, there are some powerful benefits to using annotations.
  • First, and perhaps most persuasively, we find annotations-based mappings to be far more intuitive than their XML-based alternatives, as they are immediately in the source code along with the properties with which they are associated. Most coders tend to prefer annotations because fewer files have to be kept synchronized with each other.
  • Partly as a result of this, annotations are less verbose than their XML equivalents. Let’s see the comparison e.g.
import javax.persistence.* ;
@Entity
public class Sample {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer id;
    public String name;
}
  • And compare it with an equivalent XML mapping file.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
   <class name="Sample">
      <id type="int" column="id">
         <generator class="native"/>
      </id>
      <property name="name" type="string"/>
   </class>
</hibernate-mapping>
  • Another major benefit would be that hibernate uses and supports the JPA 2 persistence annotations. If you elect not to use Hibernate-specific features in your code and annotations, you will have the freedom to deploy your entities to environments using other ORM tools that support JPA 2.
  • Finally—and perhaps a minor point—because the annotations are compiled directly into the appropriate class files, there is less risk that a missing or stale mapping file will cause problems at deployment.

3. Choosing Between the Two

In general, prefer annotations. The annotations themselves are portable across JPA implementations, and they’re well known.

  • Today, most tools can create the annotated source code directly from a database, so synchronization is less of an issue than it could be, even with a preexisting schema.
  • The XML mapping can be done either in Hibernate’s proprietary format or JPA’s standard XML configuration, which is similar but not identical; if you somehow find XML to be a preferable configuration format, you’re probably better off using the XML format from the industry-standard JPA configuration.

Let me know of your thoughts which one you prefer and why???

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.