Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate using annotations or using hibernate configuration files

I have seen many tutorials where the hibernate is implemented using annotations (basically hibernate annotations or JPA annotations). There are tutorial which mainly focuses on using the hibernate configuration files(hbm.xml files). No use of annotations at all.

Now I am little bit confused, which one is better approach ?

like image 633
Kamal Avatar asked Sep 02 '10 05:09

Kamal


1 Answers

I definitely prefer to use annotations to define my mapping metadata. And actually, annotations are the approach recommend by Hibernate developers. Quoting Gavin King in More XML than code?

Of course, Hibernate Annotations has been around since early 2005 and there is no longer any good reason for people to define mappings in XML.

Pros:

  • annotations are typically less verbose
  • annotations live with the code, they are simpler to maintain (IMO)

Cons:

  • Changing metadata requires recompilation (if this is really an issue)

In other words, use annotations if you can. If you can't (you can't change the code, for example when working with legacy code), use XML mappings.

like image 116
Pascal Thivent Avatar answered Oct 26 '22 17:10

Pascal Thivent