Andrew Powell

Into The Mind of A Solutions Architect

Andrew Powell

Hibernate: Annotations or XML Mapping?

August 15, 2008 · 7 Comments

Until recently, I've always used the XML mapping files to define my persistence model with Hibernate. I was not real keen on the idea of using meta to define persistence in the objects themselves via annotations. However, I decided to give it a shot recently. My main argument had always been that by using the XML mapping, my persistent objects are not tied to Hibernate, exclusively. When I stopped to think about it though, one thought really hit me hard: I don't use any other ORM for my Java code. Why would it matter if the meta for mappings is in the code?

This being said, I wanted to pose a question: Is there a benefit to using annotations over XML mapping (other than JVM compatibility)? Your thoughts in the comments, please.

Tags: Java · ColdFusion · Spring · Universal Mind · Hibernate · XML

7 responses so far ↓

  • 1 Sean Corfield // Aug 15, 2008 at 6:47 PM

    I'll be interested to see what people say on this question. Joe has been building Hibernate-based Groovy code using XML but I've started using annotations instead on the grounds that we aren't likely to use anything but Hibernate and it means less files to manage and no synchronization between the bean and the XML file. If there are really good reasons to use XML, I may reconsider.
  • 2 Andy Powell // Aug 15, 2008 at 10:40 PM

    Annotations also give you a better path to using pure JPA if you ever decide to go that route. XML doesn't give you that flexibility.
  • 3 Jason // Oct 19, 2008 at 12:51 AM

    annotations mean your pojos are irrevocably bound to hibernate - it may be the only orm tool you ever use, but are your objects ever reused in situations where they wont be persisted? annotations dont belong in code at all imho.
  • 4 Rich Tretola // Nov 29, 2008 at 11:40 PM

    So, which way did you go? I have also always used XML for my mappings and am curious what you decided.
  • 5 Andy Powell // Dec 1, 2008 at 11:10 AM

    I ended up going towards annotations because I finally realized that I will never use another ORM. Annotations are cleaner and keep you from having to alter your VO and your mapping file.
  • 6 Brad Bourne // Jan 29, 2009 at 4:09 PM

    Annotations are also used by JSR-299 and it's looking solid.<br />I wont argue for or against them but the option to use this kind of meta programming in cfc's would be all around awesome.
  • 7 Louis Marascio // Mar 19, 2009 at 3:19 PM

    Annotations are nice, but I've found that my POJOs get terribly ugly once I've added the various mappings. It's gotten to the point where I'm seriously considering going back to XML mapping. There are also things that you can't do with JPA, so you end up mixing JPA with Hibernate annotations, leading to even more clutter IMO. What I'm likely to end up doing is using Hibernate Validator annotations for declarative bean validation and Hibernate XML mapping for ORM mapping. Haven't tried it, so hopefully it works :).

Leave a Comment