Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Hibernate mapping file and annotation

Tags:

java

hibernate

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 annotations ? What are the pros and cons of it ? Please help me in understanding.

like image 467
Lolly Avatar asked Apr 04 '12 11:04

Lolly


People also ask

What is the hibernate mapping file?

The mapping document is an XML document having <hibernate-mapping> as the root element, which contains all the <class> elements. The <class> elements are used to define specific mappings from a Java classes to the database tables.

What is the annotation in hibernate?

Hibernate annotations are the newest way to define mappings without the use of XML file. You can use annotations in addition to or as a replacement of XML mapping metadata. Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping.

Which annotation is used for is a mapping?

JPA annotations are used in mapping java objects to the database tables, columns etc.

Why we are using JPA annotation instead of hibernate?

You use hibernate as implementation of JPA API. You should be able to change hibernate with another implementation (like EclipseLink) without changing in the code. This is why you should only use JPA annotations.


1 Answers

  • there is no functional difference. You can do (almost) the same things with both approaches
  • xml files were used before Java had annotations (added in 1.5), so they can be considered an outdated way of mapping
  • it is generally preferred to use JPA annotations rather than hibernate-specific ones; if using xml - there is a JPA xml format, which should be preferred to hibernate native one
like image 135
Bozho Avatar answered Sep 23 '22 17:09

Bozho