<p>I've got a webapp whose original code base was developed with a hand crafted hibernate mapping file.  Since then, I've become fairly proficient at 'coding' my hbm.xml file.  But all the cool kids are using annotations these days.</p>

<p>So, the question is: <strong>Is it worth the effort</strong> to refactor my code to use hibernate annotations?  Will I gain anything, other than being hip and modern?  Will I lose any of the control I have in my existing hand coded mapping file?</p>

<p>A sub-question is, <strong>how much effort will it be?</strong>  I like my databases lean and mean.  The mapping covers only a dozen domain objects, including two sets, some subclassing, and about 8 tables.</p>

<p>Thanks, dear SOpedians, in advance for your informed opinions.</p>

[{'answer_id': 82265, 'author': 'entzik', 'author_id': 12297, 'author_profile': 'https://Stackoverflow.com/users/12297', 'pm_score': 2, 'selected': False, 'text': "<p>One thing you'll gain from using annotations instead of an external mapping file is that your mapping information will be on classes and fields which improves maintainability. You add a field, you immediately add the annotation. You remove one, you also remove the annotation. you rename a class or a field, the annotation is right there and you can rename the table or column as well. you make changes in class inheritance, it's taken into account. You don't have to go and edit an external file some time later. this makes the whole thing more efficient and less error prone.</p>\n\n<p>On the other side, you'll lose the global view your mapping file used to give you.</p>\n"}, {'answer_id': 82270, 'author': 'BigJump', 'author_id': 8542, 'author_profile': 'https://Stackoverflow.com/users/8542', 'pm_score': 4, 'selected': True, 'text': '<p>"If it ain\'t broke - don\'t fix it!"</p>\n\n<p>I\'m an old fashioned POJO/POCO kind of guy anyway, but why change to annotations just to be cool?  To the best of my knowledge you can do most of the stuff as annotations, but the more complex mappings are sometimes expressed more clearly as XML.</p>\n'}, {'answer_id': 82277, 'author': 'Shimi Bandiel', 'author_id': 15100, 'author_profile': 'https://Stackoverflow.com/users/15100', 'pm_score': 0, 'selected': False, 'text': '<p>All the features are supported both in the XML and in annotations.\nYou will still be able to override your annotations with xml declaration.</p>\n\n<p>As for the effort, i think it is worth it as you will be able to see all in one place and not switch between your code and the xml file (unless of-course you are using two monitors ;) )</p>\n'}, {'answer_id': 82280, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>The only thing you'll gain from using\n  annotations</p>\n</blockquote>\n\n<p>I would probably argue that this is <strong>the</strong> thing you want to gain from using annotations. Because you don't get compile time safety with NHibernate this is the next best thing.</p>\n"}, {'answer_id': 82287, 'author': 'Darren', 'author_id': 6065, 'author_profile': 'https://Stackoverflow.com/users/6065', 'pm_score': 2, 'selected': False, 'text': "<p>As much as I like to move on to new and potentially better things I need to remember to not mess with things that aren't broken. So if having the hibernate mappings in a separate file is working for you now I wouldn't change it.</p>\n"}, {'answer_id': 83633, 'author': 'Alex Miller', 'author_id': 7671, 'author_profile': 'https://Stackoverflow.com/users/7671', 'pm_score': 2, 'selected': False, 'text': "<p>I've recently done both in a project and found:</p>\n\n<ol>\n<li>I prefer writing annotations to XML (plays well with static typing of Java, auto-complete in IDE, refactoring, etc).  I like seeing the stuff all woven together rather than going back and forth between code and XML.  </li>\n<li>Encodes db information in your classes.  Some people find that gross and unacceptable.  I can't say it bothered me.  It has to go somewhere and we're going to rebuild the WAR for a change regardless. </li>\n<li>We actually went all the way to JPA annotations but there are definitely cases where the JPA annotations are not enough, so then had to use either Hibernate annotations or config to tweak.</li>\n<li>Note that you can actually use both annotations AND hbm files.  Might be a nice hybrid that specifies the O part in annotations and R part in hbm files but sounds like more trouble than it's worth.</li>\n</ol>\n"}, {'answer_id': 89781, 'author': 'user17163', 'author_id': 17163, 'author_profile': 'https://Stackoverflow.com/users/17163', 'pm_score': 1, 'selected': False, 'text': "<p>I definitely prefer annotations, having used them both.  They are much more maintainable and since you aren't dealing with that many classes to re-map, I would say it's worth it.  The annotations make refactoring much easier.</p>\n"}, {'answer_id': 92949, 'author': 'Stu Thompson', 'author_id': 2961, 'author_profile': 'https://Stackoverflow.com/users/2961', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>"If it ain\'t broke - don\'t fix it!"</p>\n</blockquote>\n\n<p>@Macka - Thanks, I needed to hear that.  And thanks to everyone for your answers.  </p>\n\n<p>While I am in the very fortunate position of having an <strong><em>insane</em></strong> amount of professional and creative control over my work, and can bring in just about any technology, library, or tool for just about any reason (baring expensive stuff) including <em>"because all the cool kids are using it"</em>...It does not really make sense to port what amounts to a significant portion of the core of an existing project.  </p>\n\n<p>I\'ll try out Hibernate or JPA annotations with a green-field project some time.  Unfortunately, I rarely get new completely independent projects.  </p>\n'}]
['https://Stackoverflow.com/questions/82223', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2961/']