使用休眠批注或使用Hibernate配置文件 [英] hibernate using annotations or using hibernate configuration files

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

问题描述

我看过很多教程,其中Hibernate使用注释(基本上休眠批注或JPA注解)来实现。有教程,主要集中在使用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 ?

推荐答案

我肯定preFER使用注释来定义我的映射元数据。而实际上,注解是办法由Hibernate的开发者推荐。在超过code的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注解已经出现自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.

优点:


  • 注释通常更简洁

  • 注释住在code,它们易于维护(IMO)

缺点:


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

在换句话说,如果你可以使用注解。如果你不能(你不能用传统的code时改变code,例如),使用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.

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

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