In the domain model, the concept of object and value object is very important. hibernate also requires that the two be clearly distinguished, so that their ing preparation is different.
Take a pair of multiple examples as an example. The same set is used. If the object is an object, <one-to-many> and @ onetoworkflow are used. If the object is a value object, <composite-elementt> is used, @ collectionofelements. an important difference between an object and a value object is that an object has its own lifecycle, but a value object does not. It is always attached to an object. If the object does not exist, then it will die together. This is evident in hibernate ing.: @ Onetoworkflow has cascade options, but @ collectionofelements does not.
Summary:
Differences between an object and a value object:
1. The entity must have a unique identifier (ID )! To ensure that the system can clearly distinguish each entity and find it as needed. The value object does not have an ID! This is because the system never directly retrieves value objects. Value objects always belong to a certain object.
2. The object has its own independent lifecycle, but the value object does not. It is always attached to an entity. If the object does not exist, it will also die.
3. More than two entities reference a value object. This is also a guarantee for 2. If two entities have the same value, they may only have two value objects with the same value, rather than referencing the same value object.
Typical value object example: Money, address.
Database modeling principles for value objects:
1. If this value object is only used by an object and is in a one-to-one relationship, use @ embedded. to merge the data columns of the value object into the object table.
2. If there are multiple entities that will use this value object, or an object that has a group instead of a value object, you need to make the value object into a separate table. Note: This separate table is an associated table. It is not recommended in Hibernate and does not support building a value object into an absolutely independent table, then, you can refer to an object table in the form of a foreign key or associated table. See jpwh 6.3 mapping collections with annotations.