休眠:映射文件或注释? [英] Hibernate : mapping files or annotations?

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

问题描述

几天前我开始使用Hibernate时,我只是想知道:假设你从头开始一个项目。你会使用基于注解的映射还是Hibernate映射文件来生成数据库模式。

据我的理解,Hibernate映射文件提供了一些你不会发现的功能(至少不是完全相同的)。但是,我仍然觉得,现在使用Hibernate的项目宁愿使用基于注释的而不是Hibernate映射文件。



有没有人选择过映射文件而不是注释,如果是这样,是什么原因? 解决方案使用@Annotations 的优点:


  • 编译器安全 li>
  • 基于@Entity,您可以使用packagesToScan轻松识别实体与无实体

  • Spring的功能实现易于扫描

  • 将实体从包中移动到包或类重命名很简单



使用XML


  • 它不会抛出带有不必要的供应商特定的
    注释的Java类(想象一下使用JPA,JAXB,SOLRJ的java模型类
    注解)







  • 我们使用注释,但保留XML作为选项。


    As I started working with Hibernate a few days ago, I was just wondering : suppose you're starting a project from scratch. Would you use annotation-based mapping or Hibernate mapping files, to generate the database schema.

    It is my understanding that Hibernate mapping files offer some features that you won't find (at least, not the exact equivalent) with annotations. But still, I have the feeling that nowadays, projects using Hibernate would rather go for annotation-based than Hibernate mapping files.

    Has anybody ever chosen mapping files over annotations, and if so, for what reasons?

    解决方案

    What advantages I see in using @Annotations:

    • compiler-safe
    • based on @Entity you can easily distinguish entity from no-entity
    • with packagesToScan Spring's feature entites are easily scannable
    • moving entites from packages to packages or class renaming is easy

    What advantages I see in using XML:

    • it does not litters java classes with unnecessary vendor-specific annotations (imagine java model class with JPA, JAXB, SOLRJ annotations)
    • configuration in one place
    • easier to maintain as a whole

    We use annotations, but keep XML as an option.

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

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