Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Annotations instead of config file  RSS feed

arulk pillai
Ranch Hand
Posts: 3413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you cover the pros and cons of using annotation versus configuration file for O/R mapping?
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, the book focusses on how you can greatly simplify the task of doing object modelling by leveraging annotations, as opposed to using mapping files.

Annotations really are one of the greatest new features of Java, allowing you to 'markup' your Java code, without actually having to go into the various class code or method code to obtain a certain behavior.

Here's a little commentary that you can find on my website:

What is Hibernate? What is Java Persistence? What are JPA Annotations?

What's wrong with an XML mapping file?

There is nothing inherently wrong, with a mapping file, and in fact, thousands of very salacious hibernate applications that are in production use an XML mappings file, but having a big XML mapping file presents a variety of non-lethal, but certainly annoying problems, including the following:

* information about the Java class must be maintained in an external file
* XML isn't always easy to write
* with lots of classes, the XML file can become unweildly and massive
* errors in one part of the XML file can ricochet all over your Java program

Anyways, Java 5 introducted a new Java based artifact - that annotation. Basically, an annotation allows you to add detail an information about a Java class, without damaging, disturbing or changing any of the code that is actually found inside a Java class or a Java method. So, instead of using a monolithic mappings file, Hibernate with JPA annotations allows you to completely rid applications of a mapping file, and instead, you can annotate your Java classes like so:

The same code using a mapping file:

-Cameron McKenzie
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!