Difference between Hibernate mapping file and annotation

Today I tried to create an application using Hibernate as an ORM. Therefore, creating, I doubted. What is the best practice to use, hibernate mapping file (.hbm file) or annotations? What are the pros and cons of this? Please help me in understanding.

+6
source share
6 answers
  • there is no functional difference. You can do almost all the same things with both approaches.
  • xml files were used before Java had annotations (added in 1.5), so they can be considered an obsolete way of displaying
  • JPA annotations are usually preferred over non-hibernate if you use xml - there is a JPA xml format that should be preferable to native hibernation
+12
source

The question is, what is your taste - both ways can (basically) coincide, the difference is how to write.

With annotations, you have the Java member variable / receiver and the mapping directly in one place.

Xml mapping files provide a better overview of the table and its mapping.

In my opinion, xml mapping files help improve the design of the database and application. Annotations, as a rule, force the direction of the Java table class → mapping → database, which is the wrong direction (the database should always be designed first - changing database tables in the future is a lot of effort - most of the performance leaks are in poor database design - Java -classes can easily be changed at any time).

If there is one functional advantage of xml files: if you have different databases with structural differences, for example, one Oracle database and one MySQL database, maybe some differences in table names and data types, then to transfer your application from one database to another only needs to write a few new mapping files. You do not need to change one line of Java code. This is not possible with annotations.

I prefer to use xml mapping files. This is my taste.

+8
source

One good use case for the XML approach is that you save objects that were generated by another utility and therefore cannot be annotated.

Other than that, I would use annotations because it tends to yield a cleaner implementation, and you are less likely to introduce errors using typo property names.

+4
source

That's how it says in the “POINT of textbooks”

"If you are going to make the application portable for other EJB 3 ORM compatible applications, you should use annotations to represent the mapping information, but still if you need more flexibility then you should go with XML-based mapping"

For me, I would prefer an XML configuration file than annotation. Because then we can make changes with minimal code changes.

+2
source

Annotations are based on the principle of configuration agreement:

http://en.wikipedia.org/wiki/Convention_over_configuration 

while xml files are just a configuration.

There is a lot of discussion about using annotations and using configurations.

fooobar.com/questions/48558 / ...

From my point of view, I prefer annotations because they are easier to write and maintain.

0
source

Annotations are developed using the Java language and Java developer, which are easy to learn compared to XML. And one more thing in real-time table names and column names is fixed at 99%, so you don’t need to change the Java code either, but if you want to change the table and column names often, you will be moved using XML finally, the cfg.xml file is manadatary because the database may change.

-1
source

Source: https://habr.com/ru/post/912382/


All Articles