Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we use hibernate annotation?

Why is it important? what are advantage according to XML mapping? Can you explan these? thank you.

like image 558
stical Avatar asked Sep 02 '09 11:09

stical


People also ask

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.

What is the use of @entity annotation in Hibernate?

@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.

Is @column annotation necessary?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.


1 Answers

It is not something important as in "mandatory". It's a different possibility, with strength and weaknesses.

Advantages:

  • Compile-time checking : writing in Java (instead of Xml) is very user-friendly in the IDE nowadays. No more typos discovered when starting your application (incremental compilation), not that much to remember (completion)...
  • Localized with the code (class level) : instead of having to open two files (java and xml) to get the full story, with one annotated java file, you open only one file. This is less repetitive, faster in the long run.
  • Localized with the code (method or field level) : because the annotation go on a method (or field), there is no need to specify the method it belongs to. That redondant information is not given, which is shorter, and always coherent (even after a code refactoring for example). Maintenance is so much faster.
  • Tools (javadoc, other tools using reflection) can use the annotations for some other requirements.
  • Annotations are newer than the xml, the team used the input they had received at the time to provide better default values. Xml has some, but can't change much for compatibility reasons. Often, with the annotations technology, you write no annotation at all, and it works. Imagine the time-saving, especially during development.
like image 117
KLE Avatar answered Nov 18 '22 20:11

KLE