我的 Hibernate 映射中的值对象还是实体对象?

发布于 2024-08-10 09:32:41 字数 460 浏览 5 评论 0原文

我正在尝试设计一个非常简单的应用程序,但我对 Hibernate 的实体和值对象的定义有点困惑(如《Java Persistence with Hibernate》第 4 章中所定义)。

我拥有的是一个客户应用程序,他们可以下订单(一对多关系)。每个订单都有多个订单行(也是一对多)。现在,我认为客户有身份(客户编号),订单(订单号)也有身份,因此它们是实体对象?我的困惑来自于订单行。

订单行包含数量、产品编号和价格。订单行不能没有订单而存在,并且没有自己的标识,因此我将其视为值对象。但我无法使订单行成为订单表的一部分,因为订单与其订单行之间存在一对多关系。一对多关系如何与值对象的定义一起工作?来自 Hibernate 书:

“值类型的对象没有数据库标识;它属于实体实例,其持久状态嵌入到所属实体的表行中。值类型没有标识符或标识符属性”

如果有人可以消除我的困惑我真的很感激:)

I'm trying to design a pretty simple app and am getting myself a bit confused with Hibernate's definition of entity and value objects (as defined in Chapter 4 of Java Persistence with Hibernate).

What I have is an app with customers, who can place orders (one to many relationship). Each of these orders has many order lines (also one to many). Now, I think that customers have identity (customer number) and so do orders (order numbers) so they are therefore entity objects? My confusion comes in with the order lines.

An order line has quantity, product number and price. An order line can't exist without its order and has no identity of its own, therefore I see it as a value object. But I can't make order line a part of the order table as there is a one to many relationship between an order and its order lines. How do one to many relationships work with the definition of a value object? From the Hibernate book:

"An object of value type has no database identity; it belongs to an entity instance and its persistent state is embedded in the table row of the owning entity. Value types don't have identifiers or identifier properties"

If anyone can clear up my confusion I would really appreciate it :)

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

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

发布评论

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

评论(5

離人涙 2024-08-17 09:32:41

Hibernate 的文档区分了实体类型和值类型,而不是值对象。

  • 实体类型的对象:有自己的数据库标识
  • 值类型的对象:属于一个实体,其持久状态嵌入到所属实体的表行中。值类型没有标识符或标识符属性。

据我所知,本书使用的示例中包含一个表示为单个字符串的 address 和一个 user 对象,该对象包含一个地址字符串:

  • 实现为值类型(通常意味着数据库级别同一表中的列),如果删除用户,则其地址也会被删除。地址不能没有用户而存在,也不能共享。

  • 作为实体类型实现(这可能意味着使用单独的表),地址将在没有用户的情况下自行存在,并且两个用户将能够共享相同的地址。

在您的情况下,订单行不属于订单,其持久状态未嵌入订单行中(没有意义),它有自己的标识(由 orderId 和 ProductId 组成)。订单行绝对不是值类型,它是实体类型。

实际上,一旦您考虑关联(一对一、一对多等),您就肯定在操纵实体。

Hibernate's documentation makes a distinction between Entity Type and Value Type, not Value Object.

  • Object of Entity Type : has its own database identity
  • Object of Value Type : belongs to an entity, and its persistent state is embedded in the table row of the owning entity. Value types don't have identifiers or identifier properties.

As far as I can remember, the book uses a sample with an address represented as a single String and a user object, which contains an address String:

  • Implemented as a value type (which typically means a column in the same table at the database level), if the user is deleted, then so is its address. The address cannot live without the user and can't be shared.

  • Implemented as an entity type (which likely means using a separate table), the addresses would exist in their own right without the user and two users would be able to share the same address.

In your case, an order line doesn't belong to an order, its persistent state isn't embedded in the order row (doesn't make sense), it has its own identity (made of the orderId and productId). Order line is definitely not a Value Type, it is an Entity Type.

Actually, as soon as you are thinking in terms of associations (one-to-one, one-to-many, etc), you are for sure manipulating entities.

童话 2024-08-17 09:32:41

我认为你遇到的是一个相当通用的 ORM 问题。

您提到“订单行不能没有订单而存在,并且没有自己的标识”。
嗯,虽然 OrderLine 不能与 Order 一起存在,但这并不意味着它不能有标识。

以您的订单实体为例,如果没有客户,它就无法存在,但您已经将其视为实体,是吗?

因此,这是对实体的建议:
- 客户(可以没有或多个订单实体)
- 订单(可以有一个或多个 OrderLine 实体)
- 订单行

I think what you have is a rather generic ORM question.

You mentioned "An order line can't exist without its order and has no identity of its own".
Well, though OrderLine cannot exist with an Order, doesn't mean it cannot have an identity.

Take your Order entity, it cannot exists without a Customer, but you already considered it as an Entity, yea?

So, here's a suggestion for entities:
- Customer (can have none or more Order entities)
- Order (can have one or more OrderLine entities)
- OrderLine

萌化 2024-08-17 09:32:41

我认为您正在寻找复合元素。参考中有一个实际使用 Order 和purchasedItems(订单行)的示例。当 Hibernate 说它不能独立时,并不意味着它不能有自己的表,只是它总是与父元素关联:

<class name="eg.Order" .... >
  ....
  <set name="purchasedItems" table="purchase_items" lazy="true">
    <key column="order_id"/>
    <composite-element class="eg.Purchase">
      <property name="purchaseDate"/>
      <property name="price"/>
      <property name="quantity"/>
      <many-to-one name="item" class="eg.Item"/>
    </composite-element>
  </set>
</class>

来自: 依赖对象的集合

I think you're looking for a composite element. There's an example in the reference that actually uses Order and purchasedItems (order lines). When Hibernate says it can't stand alone, it doesn't mean it can't have its own table, just that its always associated with the parent element:

<class name="eg.Order" .... >
  ....
  <set name="purchasedItems" table="purchase_items" lazy="true">
    <key column="order_id"/>
    <composite-element class="eg.Purchase">
      <property name="purchaseDate"/>
      <property name="price"/>
      <property name="quantity"/>
      <many-to-one name="item" class="eg.Item"/>
    </composite-element>
  </set>
</class>

From: Collections of dependent objects

白况 2024-08-17 09:32:41

值对象是一个小对象,表示一个简单实体,其相等性不基于身份:即,当两个值对象具有相同的值时,它们相等,不一定是同一个对象

value object is a small object that represents a simple entity whose equality isn't based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same objecect

恋竹姑娘 2024-08-17 09:32:41

您可以将订单行设置为值类型,并且值类型支持一对一映射以及一对多映射。
显然,Java集合用于映射值类型与实体的*对多关系。在合适的集合内,根据需要使用元素和复合元素,如下所述:
对于实体和值类型(非JDK类型)之间的一对多关系,使用复合元素
对于一对多关系,其中值类型表要包含 JDK 类型的单个属性(例如字符串),使用元素
Java 与 Hibernate 持久化的第 6 章给出了这个概念。
详细信息请参考此链接
https://docs.jboss.org/hibernate/orm /3.5/reference/en/html/components.html

You can make order-lines as value type and value type is supported with one-to-one mapping as well as one-to-many mapping.
Obviously Java Collections are used to map the *-to-many relationship of value type with entity.Inside suitable collection,element and composite-element are used as required and is described below:
For one-to many relationship between entity and value type(non JDK type), composite-element is used.
For one-to-many relationship where value type table is to contain a single attribute of JDK type(say string), element is used.
This concept is given in chapter 6 of Java persistence with Hibernate.
For detail, refer this link
https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/components.html

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