开发者

Value object or entity object in my Hibernate mapping?

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜