域类通常会获得 JPA 或 JAXB 注释,还是两者兼而有之? [英] Do Domain Classes usually get JPA or JAXB Annotations or both?

查看:21
本文介绍了域类通常会获得 JPA 或 JAXB 注释,还是两者兼而有之?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 企业应用程序,它提供 Web 服务、域层和休眠持久层.在这种特殊情况下,我通过网络发送的对象、域对象和持久性对象之间(目前)并没有太大的区别.

I have a Java enterprise application that provides a web service, has a domain layer, and a hibernate persistence layer. In this particular case, there is not a huge difference (at the moment) between the objects I send over the wire, the domain objects, and the persistence objects.

目前,应用程序在持久性方面使用 DTO,并使用 JAXB 注释对域类进行注释.但是,我阅读和思考的越多,这似乎就越倒退!(更不用说有很多代码来支持 DTO 和域对象之间的无意识来回.)似乎大多数架构师建议将 JPA 注释放在域模型上并创建 DTO 以通过网络发送对象.

Currently the application uses DTO's on the persistence side and annotates the Domain classes with JAXB annotations. However, the more I read and think about it, the more this seems backwards! (Not to mention there is a lot of code to support the mindless back and forth between the DTO's and the Domain objects.) It seems like most architects suggest puting JPA annotations on the domain model and create DTO's for sending objects over the wire.

在我的情况下,我可以将 JAXB 和 JPA (Hibernate) 注释都放在我的域类上吗?

In my case, could I put both the JAXB and JPA (Hibernate) annotations on my domain classes?

将我的 Web 服务外观、我的域和我的持久性紧密捆绑在一起的想法似乎很容易维护,但我确实担心,因为这些可能需要及时更改.但是,为 Web 服务端创建一组 DTO 类并为持久性端跳过 DTO 会更聪明吗?

The thought of keeping my web service facade, my domain, and my persistence all tightly bundled together seems easy to maintain, but does concern me as these may need to change in time. But would it be smarter to create a set of DTO classes for the web services side and skip the DTO's for the persistence side?

推荐答案

没有功能原因不使用 JPA 和 JAXB 注释对同一个类进行注释,我有时自己做过.但是,它确实变得有点难以阅读,而且有时您希望在 JAXB 和 JPA 之间进行不同的类设计权衡.根据我的经验,这些权衡通常意味着您最终会得到两个类模型.

There's no functional reason for not annotating the same class with both JPA and JAXB annotations, I've done it myself on occasion. It does become a bit hard to read, though, and sometimes you want different class design trade-offs with JAXB and JPA. In my experience, these trade-offs usually mean you end up with two class models.

这篇关于域类通常会获得 JPA 或 JAXB 注释,还是两者兼而有之?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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