简体   繁体   English

Hibernate中与同一类的多个一对多/一对一关系

[英]Multiple one-to-many/one-to-one relationships to the same class in Hibernate

Say I have a Person, Building, and Address class. 假设我有一个Person,Building和Address类。 A person can have many addresses and a building can have one address. 一个人可以有多个地址,而建筑物可以有一个地址。 In the DB, all three have their own separate table. 在数据库中,所有这三个都有各自独立的表。 The way the Address table is linked is by using a fk_id column and a type column. 地址表的链接方式是使用fk_id列和type列。 An address for a person is stored by storing the person_id as the fk_id and setting type to "person", whereas for a building you store the building_id as fk_id and set the type to "building". 通过将person_id存储为fk_id并将类型设置为“ person”来存储人的地址,而对于建筑物,则将building_id存储为fk_id并将类型设置为“ building”。 Is there any way to map these relationships or would I need to either convert the DB to use a linking table and do a many-to-many or just use HQL to retrieve that data? 有什么方法可以映射这些关系,或者我是否需要转换数据库以使用链接表并进行多对多操作,还是仅使用HQL来检索该数据?

You can map your entities like this. 您可以像这样映射您的实体。

<class name="Address">
    <id name="addressId" column="addressId">
       <generator class="native"/>
    </id>
</class>

<class name="Person">
    <id name="personId" column="personId">
       <generator class="native"/>
    </id>
   <set name="addresses" table="PersonAddress">
       <key column="personId"/>
       <many-to-many column="addressId"
         unique="true"
         class="Address"/>
   </set>
</class>

<class name="Building">
    <id name="id" column="buildingId">
       <generator class="native"/>
    </id>
   <many-to-one name="address" 
      column="addressId" 
      unique="true"
      not-null="true"/>
</class>

U just need an extra table PersonAddress. 您只需要一个额外的表PersonAddress。

You could map this as so: 您可以这样映射:

@Entity
public class Address {
  @ManyToOne @JoinColumn(name="person_id")
  private Person person;

  @OneToOne @JoinColumn(name="building_id")
  private Building building;
}

@Entity
public class Person {
  @OneToMany(mappedBy="person", targetEntity=Address.class)
  private Set<Address> addresses;
}

@Entity
public class Building {
  @OneToOne(mappedBy="building")
  private Address address;
}

In this example, address could have either a Person or a Building (it should not have both). 在此示例中,地址可以具有“人”或“建筑物”(不应同时具有两者)。 Also, Address is considered the owner of the relationship, and Person or Building is the "inverse" side of that relationship. 同样,地址被认为是关系的所有者,人或建筑物是该关系的“反面”。

That being said, to create the object graph, you could still use cascading saves (the "cascade" attribute on the annotations) so that you could create all the objects in memory, and simply save the Person or Building, and it should automagically save the Address objects along with it. 话虽如此,要创建对象图,您仍然可以使用级联保存(批注中的“ cascade”属性),以便可以在内存中创建所有对象,并简单地保存Person或Building,它应该自动保存地址对象以及它。

找到答案后,所需要做的就是在集合上添加一个“ where”条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM