使用注释或使用休眠配置文件进行休眠 [英] hibernate using annotations or using hibernate configuration files

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

问题描述

我看过很多教程,其中使用注释(基本上是 hibernate 注释或 JPA 注释)实现了 hibernate.有一些教程主要侧重于使用 hibernate 配置文件(hbm.xml 文件).根本不使用注释.

I have seen many tutorials where the hibernate is implemented using annotations (basically hibernate annotations or JPA annotations). There are tutorial which mainly focuses on using the hibernate configuration files(hbm.xml files). No use of annotations at all.

现在我有点困惑,哪种方法更好?

Now I am little bit confused, which one is better approach ?

推荐答案

我绝对更喜欢使用注释来定义我的映射元数据.实际上,注释是 Hibernate 开发人员推荐的方法.在 XML 多于代码?

I definitely prefer to use annotations to define my mapping metadata. And actually, annotations are the approach recommend by Hibernate developers. Quoting Gavin King in More XML than code?

当然,Hibernate Annotations 早在 2005 年就已经存在,人们不再有任何理由在 XML 中定义映射.

Of course, Hibernate Annotations has been around since early 2005 and there is no longer any good reason for people to define mappings in XML.

优点:

  • 注释通常不那么冗长
  • 注释与代码一起存在,它们更易于维护 (IMO)

缺点:

  • 更改元数据需要重新编译(如果这确实是一个问题)

换句话说,如果可以,请使用注释.如果您不能(您不能更改代码,例如在处理遗留代码时),请使用 XML 映射.

In other words, use annotations if you can. If you can't (you can't change the code, for example when working with legacy code), use XML mappings.

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

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