我的 Hibernate 映射中的值对象或实体对象? [英] Value object or entity object in my Hibernate mapping?

查看:21
本文介绍了我的 Hibernate 映射中的值对象或实体对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

订单行包含数量、产品编号和价格.订单行没有订单就不能存在,也没有自己的身份,因此我将其视为值对象.但是我不能将订单行作为订单表的一部分,因为订单与其订单行之间存在一对多的关系.一对多关系如何处理值对象的定义?来自 Hibernate 的书:

<块引用>

值类型的对象没有数据库标识;它属于实体实例,其持久状态嵌入在拥有实体的表行中.值类型没有标识符或标识符属性"

如果有人能解决我的困惑,我将不胜感激:)

解决方案

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

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

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

  • 实现为值类型(通常是指数据库级别的同一个表中的列),如果用户被删除,则其地址也将被删除.该地址无法在没有用户的情况下存在且无法共享.

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

    莉>

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

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

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 :)

解决方案

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.

这篇关于我的 Hibernate 映射中的值对象或实体对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆