java DAO 设计模式并在多个表中使用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2504887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:31:53  来源:igfitidea点击:

DAO design pattern and using it across multiple tables

javadesign-patternsjdbcjakarta-ee

提问by Casey

I'm looking for feedback on the Data Access Objectdesign pattern and using it when you have to access data across multiple tables. It seems like that pattern, which has a DAO for each table along with a Data Transfer Object (DTO) that represents a single row, isn't too useful for when dealing with data from multiple tables. I was thinking about creating a composite DAO and corresponding DTO that would return the result of, let's say performing a join on two tables. This way I can use SQL to grab all the data instead of first grabbing data from one using one DAO and than the second table using the second DAO, and than composing them together in Java.

我正在寻找有关数据访问对象设计模式的反馈,并在您必须跨多个表访问数据时使用它。似乎这种模式(每个表都有一个 DAO 以及一个表示单行的数据传输对象 (DTO))在处理来自多个表的数据时不太有用。我正在考虑创建一个复合 DAO 和相应的 DTO,它将返回结果,假设在两个表上执行连接。这样我就可以使用 SQL 来获取所有数据,而不是首先使用一个 DAO 从一个表中获取数据,然后使用第二个 DAO 从第二个表中获取数据,而不是在 Java 中将它们组合在一起。

Is there a better solution? And no, I'm not able to move to Hibernate or another ORM tool at the moment. Just straight JDBC for this project.

有更好的解决方案吗?不,我目前无法转移到 Hibernate 或其他 ORM 工具。这个项目只是直接的JDBC。

采纳答案by pkananen

I would agree with your approach. My DAOs tend to be aligned more at the object level, rather than from a DB Table perspective. I may manage more than one object through a DAO, but they will very likely be closely related. There is no reason not to have SQL accessing two tables living in one DAO.

我会同意你的方法。我的 DAO 更倾向于在对象级别对齐,而不是从数据库表的角度来看。我可能通过一个 DAO 管理多个对象,但它们很可能是密切相关的。没有理由不让 SQL 访问位于一个 DAO 中的两个表。

And for the record, I have banished the acronym DTO from my vocabulary and code.

作为记录,我已经从我的词汇表和代码中删除了首字母缩略词 DTO。

回答by Nazar Merza

Ideally, how you store your data in a database, and then how you access them, should be derived from the nature of the relationship among the domain entities in your domain model. That is, Relational Model should follow from Domain Model. For example, if you have two entities, say, User and Address.

理想情况下,您如何将数据存储在数据库中,然后如何访问它们,应该从域模型中域实体之间的关系的性质中推导出来。也就是说,关系模型应该遵循领域模型。例如,如果您有两个实体,例如用户和地址。

Scenario #1: Address are never accessed independently, they are always an attribute of User. In this case, Address is a Value Object and User is an Entity, and there are guides on how to store this relationship. One way is to store Address attributes of Address alongside of attributes of User, in a single table. In this case, UserDao will handle both objects.

场景#1:地址永远不会被独立访问,它们始终是用户的一个属性。在这种情况下,地址是一个值对象,用户是一个实体,并且有关于如何存储这种关系的指南。一种方法是将 Address 的 Address 属性与 User 的属性一起存储在单个表中。在这种情况下,UserDao 将处理这两个对象。

Scenario #2: Address can be associated to a User, but also can be separate on its own, an entity. In this case, an approach different from the first one is needed. You may have a separate DAO and table for the Address type.

场景#2:地址可以与用户相关联,但也可以单独作为一个实体。在这种情况下,需要一种不同于第一种方法的方法。您可能有一个单独的 DAO 和地址类型表。

My point is, that more often this important idea is ignored that Domain Model should be the core of the application, driving other layers.

我的观点是,这个重要的想法经常被忽略,即域模型应该是应用程序的核心,驱动其他层。

For instance, if your domain model is properly define and you are well aware of the type of entities you have and the relationship among them, then your persistence (relational tables and their relationships, your DAOs, etc) will evolve as a very logical consequence of what you have in the domain model.

例如,如果您的领域模型定义正确,并且您非常了解您拥有的实体类型以及它们之间的关系,那么您的持久性(关系表及其关系、您的 DAO 等)将演变为一个非常合乎逻辑的结果您在域模型中拥有的内容。

In other words, if you spend some time studying your model, you will be able to trace your problem in determining how to organize your DAOs to a place in the domain model. If you can clearly define the type of the objects and the nature of relationship among them in the domain model, it will, help you resolve your problem in DAL layer.

换句话说,如果您花一些时间研究您的模型,您将能够在确定如何将您的 DAO 组织到域模型中的某个位置时跟踪您的问题。如果你能在领域模型中清楚地定义对象的类型和它们之间关系的性质,它将帮助你解决DAL层中的问题。