Hibernate : mapping files or annotations?

后端 未结 2 1512
旧巷少年郎
旧巷少年郎 2021-02-04 15:06

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 m

相关标签:
2条回答
  • 2021-02-04 15:49

    Go ahead with annotations any day. The XML configurations where really over used and saving the metadata inside the class is a good viable option.

    Annotations will help you map the relationships better and it will align you to the JPA standard as hibernate uses mostly JPA annotations. There are no real problems using annotations and there is not much trade-off either. It has superior advantages over the XML based configurations. There might be few hacks missing when you use annotations but they will come along.

    It is even possible to use annotations for new classes in a legacy project that has XML based mapping as told here.

    0 讨论(0)
  • 2021-02-04 16:12

    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.

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