views:

298

answers:

3

Hey everyone,

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

+1  A: 

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

o.k.w
+1  A: 

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 raw (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.

Pascal Thivent
A: 

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

Brian Deterling