1

From the DDD book by Eric Evans:

VALUE OBJECTS can even reference ENTITIES. For example, if I ask an online map service for a scenic driving route from San Francisco to Los Angeles, it might derive a Route object linking L.A. and San Francisco via the Pacific Coast Highway. That Route object would be a VALUE, even though the three objects it references (two cities and a highway) are all ENTITIES.

page #98

In Hibernate if I have a value object I can map it with as a component. What if I want to reference an entity form that component?

Example:

  • I have a Users table.
  • Each User can have many addresses.
  • I create a addresses table, but I treat the addresses as value objects in my domain.
  • Each address have a type (work address, home address,...etc.)
  • I create a address type table as a look up and treat it as an entity in my domain.
  • An address should have a reference to it type.

How can I achieve that?

3
  • Declare an one-to-one relationship in the componnent? Commented Nov 11, 2013 at 1:29
  • Why is address type an entity? Do you care about its identity? Commented Nov 11, 2013 at 18:25
  • @JefClaes I was influenced by this question on SO. Do you think it's better modeled as a VO? Commented Nov 11, 2013 at 20:35

1 Answer 1

1

See the documentation.

The <component> element maps properties of a child object to columns of the table of a parent class. Components may, in turn, declare their own properties, components or collections. See "Components" below.

<component> <!-- NOTE: I'm omitting the attributes. See docs for details on these. --> <property ...../> <many-to-one .... /> ........ </component> 

Notice the example property and many-to-one in the code above. To create a reference to another entity, you simply use many-to-one inside the component, just as you would outside the component.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.