Andrew Powell

Into The Mind of A Solutions Architect

Andrew Powell

Hibernate: Annotations or XML Mapping?

August 15, 2008 · 12 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: ColdFusion · Hibernate · Java · Spring · Universal Mind · XML

12 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 :).
  • 8 Andrey Yegorov // Feb 28, 2010 at 6:52 AM

    I like the overall idea of using the annotations, but I see some limitations:<br />As was said by Jason: &quot;annotations mean your pojos are irrevocably bound to hibernate&quot;; we got persisted objects, but thist fact could change in future.<br />2. Xml mapping is more flexible way in the case the database structure changes. There is no need to get pojo source file, change annotations in it and recompile it. All you need is to edit xml file.<br />So, I think mapping java objects to database should be store in configuration file like xml, but not in source file.
  • 9 lwpro2 // Sep 13, 2010 at 3:09 AM

    I also prefer xml mapping, dont someday wanna switch away from hibernate, but got huge lot stuff to modify.
  • 10 Jeroen Ost // Sep 21, 2010 at 8:45 AM

    If the POJO's are also needed on the client side (e.g. a standalone Swing application), you definitely don't want the persistence cluttering the java class. And if you use OSGI and separate the objects in a bundle from the persistency implementation so your application only talks to the POJO objects bundle (allowing you to reuse the bundle elsewhere, perhaps with a different persistency bundle), the same applies. In those cases hbm.xml looks like the best solution, even though annotations make it easier to read somebody else's code.
  • 11 Lisa // Jan 5, 2011 at 2:32 PM

    Aren't you limited to one entity per class with annotations? That seems to be the case. If so it is a severe limitation in my opinion. What if every part of your application that touches on the X table doesn't need every property from the X table? You might want entities that are lightly populated to improve performance. If you only have one, you have to get the whole massively populated object every time.
  • 12 Oscar Calderon // Jan 5, 2011 at 2:56 PM

    What do you think about application performance when you use annotations VS mapping files? I read in some forums and blogs different opinions, some guys says that using annotations have in impact on performance because the classes must be scanned looking for annotations in the runtime, others says that is the same.

Leave a Comment