Value object or entity object in my Hibernate mapping?

不打扰是莪最后的温柔 提交于 2019-11-29 08:02:18
Pascal Thivent

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.

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!