Hibernate annotation or XML configuration

后端 未结 3 1103
醉梦人生
醉梦人生 2020-11-30 08:57

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

相关标签:
3条回答
  • 2020-11-30 09:06

    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.

    0 讨论(0)
  • 2020-11-30 09:12

    Before annotation there was only one way to do that - XML. If you use annotation, you will have all in one place, therefore understanding and maintaning it will be easier.

    0 讨论(0)
  • 2020-11-30 09:24

    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.

    0 讨论(0)
提交回复
热议问题