Hibernate 注释和 DTO
我有一个关于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个问题或所谓的“独立性”与易用性的问题。您选择使用 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.
当您使用 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).
如果我理解正确的话,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.
来自维基百科:
因此,DTO 是从实体创建并由表示层使用的对象(例如,JSP 应该访问 DTO,而不是直接访问实体)。因此,您不应该注释 DTO,而应该注释实体类,然后提供将实体转换为 DTO 的代码。
From Wikipedia:
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.