gpt4 book ai didi

java - 什么是持久化对象?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:40:15 27 4
gpt4 key购买 nike

我正在阅读 Java EE 教程和 here我在开头看到这句话:

An entity is a lightweight persistence domain object.

我已经搜索过persistence object 但找不到明确的解释。

持久域对象到底是什么?

最佳答案

Java EE 假设一个叫做 Domain Model 的东西.域模型由表示实体的对象组成,其中 Entity是具有与业务相关的身份的东西。 (例如,如果您在银行工作,您的领域可能涉及账户、客户、 Assets 和贷款等内容)。

这里引用 Bauer 和 King 的 Java Persistence with Hibernate 描述领域模型:

3.1.1. Analyzing the business domain

A software development effort begins with analysis of the problem domain (assuming that no legacy code or legacy database already exists).

At this stage, you, with the help of problem domain experts, identify the main entities that are relevant to the software system. Entities are usually notions understood by users of the system: payment, customer, order, item, bid, and so forth. Some entities may be abstractions of less concrete things the user thinks about, such as a pricing algorithm, but even these would usually be understandable to the user. All these entities are found in the conceptual view of the business, which we sometimes call a business model. Developers and architects of object-oriented software analyze the business model and create an object-oriented model, still at the conceptual level (no Java code). This model may be as simple as a mental image existing only in the mind of the developer, or it may be as elaborate as a UML class diagram created by a computer-aided software engineering (CASE) tool like ArgoUML or TogetherJ. A simple model expressed in UML is shown in figure 3.1.

This model contains entities that you're bound to find in any typical auction system: category, item, and user. The entities and their relationships (and perhaps their attributes) are all represented by this model of the problem domain. We call this kind of object-oriented model of entities from the problem domain, encompassing only those entities that are of interest to the user, a domain model. It's an abstract view of the real world.

The motivating goal behind the analysis and design of a domain model is to capture the essence of the business information for the application's purpose.

理想情况下(在一种称为 Domain-Driven Design 的方法中)这些域对象具有 2 个特征:它们不知道诸如持久性或事务之类的基础设施问题,并且它们包含实现状态转换的逻辑,这些状态转换在它们在过程中被操作时发生业务处理;这些组合意味着业务逻辑可以独立于基础架构进行测试。在现实世界中,更常见的是看到 anemic domain objects其中不包含任何业务逻辑,业务逻辑都以 transaction scripts 结尾.

无论如何,您的想法是拥有一个由持久实体组成的领域模型。有某种配置(注释或 XML 文件或其他)将实体及其属性映射到数据库中的表和列,并映射实体之间的关系。有一个对象关系映射器(JPA 是实现 ORM 的标准,Hibernate 就是这样一种实现)它知道如何在数据库表示和对象图表示之间来回转换数据,以便开发人员可以操纵对象数据库行。

对于那些声称业务逻辑不应成为域模型的一部分的人,这里是 Java Persistence with Hibernate 一书的另一段引述,在第 3.1.2 节:

The entities in a domain model should encapsulate state and behavior. For example, the User entity should define the name and address of a customer and the logic required to calculate the shipping costs for items (to this particular customer). The domain model is a rich object model, with complex associations, interactions, and inheritance relationships. An interesting and detailed discussion of object-oriented techniques for working with domain models can be found in Patterns of Enterprise Application Architecture (Fowler, 2003) or in Domain-Driven Design (Evans, 2003).

In this book, we won't have much to say about business rules or about the behavior of our domain model. This isn't because we consider it unimportant; rather, this concern is mostly orthogonal to the problem of persistence. It's the state of our entities that is persistent, so we concentrate our discussion on how to best represent state in our domain model, not on how to represent behavior. For example, in this book, we aren't interested in how tax for sold items is calculated or how the system may approve a new user account. We're more interested in how the relationship between users and the items they sell is represented and made persistent. We'll revisit this issue in later chapters, whenever we have a closer look at layered application design and the separation of logic and data access.

很明显,Hibernate 开发人员将其视为一种可行的替代方法,尽管它似乎不是典型企业开发中的常用方法。

关于java - 什么是持久化对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16041898/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com