Hibernate 注释或 XML 配置 [英] Hibernate annotation or XML configuration

查看:26
本文介绍了Hibernate 注释或 XML 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Hibernate 开始了一个新项目.Hibernate annotation 是更好的选择还是 Hibernate XML 映射更好?

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

我已经将 Hibernate 与 XML 配置一起使用,但我不知道注释.

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

  1. 在进行基于注释的实现时有什么问题吗?
  2. 使用基于注释的应用程序维护更容易还是更复杂?
  3. 哪一个更好(即注解或XML映射),哪一个被广泛使用?为什么?
  4. 如果我在不同级别(即生产或集成或开发级别)使用不同的数据库,我是否需要更改代码?

注意:我不是在比较两种技术,但我想知道在具有最新技术的应用程序中使用哪一种(即两种方式).

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.

推荐答案

注释是新应用程序的更好选择,因为它使应用程序摆脱了 XML 文件.它也更少的代码和维护很容易.它还有助于重构应用程序(使用 Eclipse/NetBeans 等 IDE),这是 XML 的问题,因为您需要手动更改它.

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.

答案:

  1. 没问题,您可以使用最新的 JSR 330 API(依赖Java 注入),这有助于 JSF 托管 bean 的相同注释(使用 @Named 而不是 @managedbean),Spring IoC层和 Hibernate,这样整个层将有一个单一的注释概念,而不是特定于框架的.

  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.

维护很容易,尤其是代码重构、更少或没有 XML 文件和新的学习.在 Hibernate 中使用基于 JPA 的注释.您可以在 google 中找到一些示例.

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.

广泛使用基于 XML 的,因为它从一开始就存在,但对于新的应用程序注释更好,更成熟.

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

对于任何数据库的更改,注释或 XML 中的代码将保持不变,只需更改属性文件值即可.

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

这篇关于Hibernate 注释或 XML 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆