Rediscussion on value objects and entities

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.