Songo
Songo

Reputation: 5736

How do reference an Entity from a value object in Hibernate/NHibernate?

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:

How can I achieve that?

Upvotes: 1

Views: 501

Answers (1)

Daniel Schilling
Daniel Schilling

Reputation: 4967

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.

Upvotes: 1

Related Questions