有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

c#如何从Hibernate/NHibernate中的值对象引用实体?

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

在Hibernate中,如果我有一个值对象,我可以将其映射为组件。如果我想引用该组件的实体,该怎么办

例如:

  • 我有一张用户表
  • 每个用户可以有许多地址
  • 我创建了一个地址表,但我将地址视为域中的值对象
  • 每个地址都有一个类型(工作地址、家庭地址等)
  • 我创建一个地址类型表作为查找,并将其视为域中的一个实体
  • 地址应具有对其类型的引用

我怎样才能做到这一点


共 (1) 个答案

  1. # 1 楼答案

    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>
    

    注意上面代码中的示例propertymany-to-one。要创建对另一个实体的引用,只需在组件内部使用many-to-one,就像在组件外部一样