Hibernate 注释和 DTO

发布于 2024-12-14 11:56:48 字数 260 浏览 6 评论 0原文

我有一个关于 Hibernate 注释和 DAO 模式的使用的设计问题。 DTO 应该代表数据模型的实体。 DAO 是定义这些 DTO 上的操作的接口。 DAO 实现是实现 DAO 接口的类,并实现这些接口定义的操作(例如使用 Hibernate/MySQL)。 我的问题是:在这种情况下如何使用 Hibernate 注释?如果我直接注释 DTO,我会将 DTO 与 Hibernate 框架结合起来,我认为这是一个不好的做法。

也许这是一个简单的问题,但问题很有趣。

谢谢

I have a design question about the use of Hibernate annotations and DAO pattern. The DTO are supposed to represent entities of the data model. The DAOs are interfaces that define operations on these DTOs. The DAOs implementations are classes that implement the DAO interfaces, and implement the operations defined by these interfaces (for example using Hibernate/MySQL).
My question is : How can I use Hibernate annotations in this case? If I annotate directly the DTO, I couple my DTO with the Hibernate framework which is a bad practice I think.

Maybe it's a simple question but the problem is interesting.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聊慰 2024-12-21 11:56:48

这是一个问题或所谓的“独立性”与易用性的问题。您选择使用 Hibernate 或其他 JPA 引擎,并且您确实需要在实体上添加注释,并在类路径中添加 hibernate jar 才能使用它们。或者你选择完全独立于JPA/Hibernate,但你必须自己实现整个持久化。

我的观点是,仅仅为了避免类路径中出现一些 jar 而牺牲 JPA 提供的易用性和生产力收益是一个糟糕的选择。但是YMMV。

It's a matter or supposed "independance" vs. ease of use. Either you choose to use Hibernate or another JPA engine, and you indeed need to have annotations on your entities and have the hibernate jars in your classpath to use them. Or you choose to be completely independant of JPA/Hibernate, but you have to implement the whole persistence yourself.

My opinion is that sacrificing the ease of use and productivity gains offered by JPA just to avoid some jars in the classpath is a bad choice. But YMMV.

江湖正好 2024-12-21 11:56:48

当您使用 javax.persistence 包中的注释时,您将您的代码与 Hibernate 耦合(但是,当您使用 org.hibernate 注释时,您会这样做,因为它们依赖于第 3 方库)。

请注意,注释只是元信息,不会影响您的设计(您不会强制类实现像接口那样的方法),您只需为某些目的使用附加信息来注释它们。

只要注释属于标准化 java api(在本例中为 javax.persistence),注释类的客户端就不会被迫将其代码与其他依赖项耦合起来。

When you are using annotations from javax.persistence package you are NOT coupling your code with Hibernate (you would however when using org.hibernate annotations, because they rely on 3rd party libraries).

Note that annotations are just meta information not affecting your design (you don't force classes to implement methods like with interfaces), you just annotate them with additional information for certain purposes.

Client of annotated classes isn't forced to couple his code with additional dependencies as long as annotations belong to standardized java api (javax.persistence in this case).

何以心动 2024-12-21 11:56:48

如果我理解正确的话,DTO 是 Hibernate 和数据库表之间的对象关系映射,如果是这种情况,我相信您最好对 DTO 对象进行注释。

If I understand you correctly, DTOs are object-relation mapping between Hibernate and your database tables, if that is the case I believe you better have your DTO objects annotated.

小草泠泠 2024-12-21 11:56:48

来自维基百科:

在传统的 EJB(Enterprise JavaBeans)架构中,DTO 有双重用途:首先,它们解决了 ejb 3.0 之前的实体 bean 不可序列化的问题;其次,它们隐式定义了一个组装阶段,在该阶段中,视图要使用的所有数据都将被获取并编组到 DTO 中,然后再将控制权返回到表示层。[需要引用];使用 DTO 的第三个原因可能是应用程序的某些层不应该能够访问底层数据访问对象,从而更改数据。

因此,DTO 是从实体创建并由表示层使用的对象(例如,JSP 应该访问 DTO,而不是直接访问实体)。因此,您不应该注释 DTO,而应该注释实体类,然后提供将实体转换为 DTO 的代码。

From Wikipedia:

In a traditional EJB (Enterprise JavaBeans) architecture, DTOs serve dual purposes: first, they work around the problem that entity beans pre-ejb 3.0 are not serializable; second, they implicitly define an assembly phase where all data to be used by the view are fetched and marshalled into the DTOs before returning control to the presentation tier.[citation needed]; a third reason of using DTOs could be that certain layers of the application should not be able to access the underlying data access objects, and hence change the data.

Thus DTO are the objects which are created from entities and are used by the presentation layer (JSP, for example, should access DTO-s and not the entities directly). So, you should annotate not the DTO-s but your Entity classes, and then provide the code to translate Entities to DTO-s.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文