Value object or object of object in my Hibernate mapping?

I'm trying to create a fairly simple application, and I'm a little confused about defining entity objects and Hibernate values ​​(as defined in Chapter 4 of Java Persistence with Hibernate).

I have an application with clients who can place orders (from one to many relationships). Each of these orders has many order lines (also from one to many). Now, I think that customers have an identity (customer number) as well as orders (order numbers), so are they entity entities? My confusion comes with order lines.

The order line has quantity, product number and price. An order line cannot exist without its order and has no eigenvalue, so I consider it as an object of value. But I cannot make the order line part of the order table, because there is a one-to-many relationship between the order and its ordinal lines. How to work with one or several relationships with the definition of an object of value? From the Hibernate book:

"An object of type value does not have a database identifier, it belongs to an instance of the object, and its constant state is embedded in the table row of the owner object. Value types do not have identifiers or identifier properties"

If anyone can clear my confusion, I would really appreciate it :)

+4
source share
5 answers

The Hibernate documentation makes a distinction between an Entity and a Value Type, not a Value Object.

  • Object of Entity Type: has its own database identifier
  • An object of type value: belongs to the entity, and its constant state is embedded in the table row of the owner object. Value types do not have identifiers or identifier properties.

As far as I remember, the book uses a sample with address , presented as a single String object and user , which contains the address of String:

  • Implemented as a value type (usually this means a column in the same table at the database level), if the user is deleted, then his address. An address cannot live without a user and cannot be shared.

  • Implemented as an entity type (which probably means using a separate table), the addresses will exist on their own without a user, and two users will be able to use the same address.

In your case, the order line is not related to order, its constant state is not built into the order line (it makes no sense), it has its own identification (consisting of orderId and productId), the order line is definitely not a value type, it is an object type.

Actually, as soon as you think about alliances (one-to-one, one-to-many, etc.), you will surely manipulate entities.

+6
source

I think you have a pretty general ORM question.

You mentioned: "An order line cannot exist without its order and does not have its own identity."
Well, although OrderLine cannot exist with the Order, it does not mean that it cannot have an identifier.

Take the Order object, it cannot exist without the Client, but you already considered it as Entity, right?

So here is a sentence for entities:
- Client (may not have one or more subjects of the order)
- Order (may have one or more OrderLine objects)
- OrderLine

+1
source

I think you are looking for an integral element. There's an example in a link that actually uses Order and purchaseItems (order lines). When Hibernate says that he cannot stand alone, this does not mean that he cannot have his own table, just that he is always associated with the parent:

 <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

+1
source

the value object is a small object that is a simple object whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same objecect

0
source

You can create order lines, as the value type and value type are supported by one-to-one mapping, as well as one-to-many mapping. Obviously, Java collections are used to map a one-to-many relationship between a value type and an entity. Within an appropriate collection, an element and a composite element are used as needed and are described below: For a one-to-many relationship between an entity and a value type (non-JDK type), a composite element is used . For a one-to-many relationship, where the value type table contains one JDK type attribute (such as a string), an element is used. This concept is given in Chapter 6 on Java Retention with Hibernate. See the link https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/components.html for details

0
source

All Articles