Today I was trying to create a application using Hibernate as ORM. So while creating I had a doubt. What is the best practice to use, Hibernate mapping file (.hbm file) or a
This is how it says in "TUTORIALS POINT"
"If you going to make your application portable to other EJB 3 compliant ORM applications, you must use annotations to represent the mapping information but still if you want greater flexibility then you should go with XML-based mappings"
For me I would prefer XML configuration file, than annotation. Because then we can do changes, with minimum code changes.
The question is what is your taste - both ways can do (mostly) the same, the difference is how to write.
With annotations you have the Java member variable/getter and the mapping directly together at one place.
Xml mapping files give a better overview over the table and its mapping.
In my opinion xml mapping files help for a better design of the database and application. Annonations tend to force the direction Java class -> mapping -> database table, which is the wrong direction (the database always should be designed first - changing database tables later is a lot of effort - most performance leaks are in a bad database design - Java classes easily can be changed any time).
There if one functional advantage of xml files: If you have different databases with structural differences, for example one Oracle database and one MySQL database, perhaps some differences in table names and data types, then for porting your application from one database to another you only need to write some new mapping files. You do not need to change a single line of Java code. This is not possible with annotations.
I prefer to use xml mapping files. That is my taste.
One good use-case for using the XML approach is if you are persisting objects that have been generated by another utility and therefore cannot be annotated.
Other than that, I would use annotations as it tends to lend itself to a cleaner implementation and you're less likely to introduce bugs by misspelling property names.
Annotations are based on the principle of convention over configuration:
http://en.wikipedia.org/wiki/Convention_over_configuration
while xml files are just configuration.
There are many discussions about using annotations vs using configuration.
stackoverflow link
From my point of view, I prefer annotations because it is easier to write and to maintain.
Annotations are developed with java language and java developer easy to learn when compared with XML. And one more point in real time table names and column names are fixed 99%, so no need to change java code also but if you want to change table names and column names frequently you will move with XML finally cfg.xml file is manadatary because database may change.