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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With