Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate annotation or XML configuration

I have started a new project with Hibernate. Is Hibernate annotation a better choice or is Hibernate XML mapping better?

I have used Hibernate with XML configuration, but I have no idea about annotation.

  1. are there any issues while going for annotation based implementation?
  2. is maintenance of an application easier or more complicated with annotation based?
  3. which one is better (that is, annotation or XML mapping) and which one is is widely used? Why?
  4. If I use a different database at a different level, that is, production or integration or development level, do I need to go for change of code?

Note: I am not comparing two technologies, but I want to know which one (that is, out of two ways) to be used in the application with the latest technology.

like image 208
TechFind Avatar asked Aug 22 '11 11:08

TechFind


People also ask

Which is better annotation or XML in spring?

From my own experience annotations better than xml configuration. I think in any case you can override xmls and use annotations. Also Spring 4 give us a huge support for annotations, we can override security from xml to annotations e.t.c, so we will have not 100 lines xml but 10 lines Java Code.

What is XML configuration in hibernate?

These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file. Hibernate facilitates to provide the configurations either in an XML file (like hibernate. cfg.

Is it necessary to use hibernate cfg XML for configuration?

Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.

What is difference between XML and annotation in spring?

React + Spring Boot Microservices and Spring So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration. Annotation injection is performed before XML injection.


2 Answers

Annotations are a better choice for a new application, since it makes the application free from XML files. It is also less code and maintenance is easy. It also helps for refactoring the application (with IDE like Eclipse/NetBeans, etc.), which is the problem with XML since you need to change it manually.

Answers:

  1. No issues, you can use the latest JSR 330 API (dependency injection for Java) which helps to the same annotation for a JSF managed bean (use @Named instead of @managedbean), the Spring IoC layer and Hibernate, so that whole layer you will have a single annotation concept instead of a framework specific.

  2. Maintenance is easy especially refactoring of code, fewer or no XML files and new learning. Use JPA based annotation with Hibernate. You can find some sample in google.

  3. Widely use XML based since it was there from begining, but for a new application annotation is better which is more mature.

  4. For change to any database, the code will remain the same in annotation or XML, only you need to change the property file values.

like image 99
AKB Avatar answered Sep 28 '22 05:09

AKB


Annotations are newer, and are the standard way of doing for JPA applications.

I find them easier to use than XML, but it's a matter of taste and experience. Using them would make you

  • learn something new
  • ready to start a JPA-based project (that could use another implementation)

I've never been in a situation where I needed XML because annotations didn't allow me to do what I wanted.

like image 37
JB Nizet Avatar answered Sep 28 '22 05:09

JB Nizet