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

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

问题描述

我试图设计一个非常简单的应用程序,并且让我对Hibernate对实体和值对象的定义(如Java Persistence with Hibernate的第4章中定义)有点困惑。



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



订单行包含数量,产品编号和价格。订单行在没有订单的情况下不能存在,并且没有自己的身份,因此我将其视为价值对象。但是我不能让订单行成为订单表的一部分,因为订单和订单行之间存在一对多的关系。一对多关系如何与值对象的定义一起工作?从Hibernate书中:


值类型的对象没有数据库标识;它属于实体实例,其持久状态是嵌入的如果任何人都可以清除我的困惑,那么我们就可以通过这个方法来解决这个问题。我真的很感激它:)

解决方案

Hibernate的文档区分了 Entity Type 值类型,而不是值对象。



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


据我记忆, book使用一个带有地址表示为单个字符串和用户对象的示例,其中包含地址字符串:




  • 实现为值类型(通常表示数据库级别的同一表中的列),如果用户被删除,它的地址也是如此。

  • 实施为实体类型(可能意味着使用单独的表),地址将会存在在他们自己的权利,没有用户和两个用户将能够共享相同的地址。 ,订单行不属于某个订单,其持久状态未嵌入到订单行中(没有意义),它具有自己的标识(由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天全站免登陆