【问题标题】:Is it possible to have the same "OneToOne"-Relationship twice in hibernate?是否有可能在休眠中具有相同的“OneToOne”-Relationship 两次?
【发布时间】:2015-07-07 09:08:00
【问题描述】:

我不知道如何在标题中描述我的问题,但我希望它能做到。 这是我的情况。

我使用休眠将我的实体映射到数据库表。

我有一个这样的实体:

@Entity
@Table(name = "EX.EXAMPLE")
public abstract class Entity
{
  private CustomEntity customEntity;
  public static final String CUSTOM_ENTITY = "customEntity";

  @OneToOne(cascade = CascadeType.ALL, mappedBy = CustomEntity.ENTITY, fetch = FetchType.LAZY)
  @Fetch(FetchMode.SELECT)
  public CustomEntity getCustomEntity()
  {
    return this.customEntity;
  }
}

还有我的自定义实体

    @Entity
    @Table(name = "EX.EXAMPLE2")
    public class CustomEntity 
    {
      private Entity entity;
      public static final String ENTITY = "entity";

      @OneToOne
      @JoinColumn(name = "ID_ENTITY", nullable = true)
      public Entity getEntity()
      {
        return this.ntity;
      }
    }

所以这是我的问题:是否可以向 Entity 添加另一个 CustomEntity 关系?我如何映射它? 例如我的意思:

    @Entity
    @Table(name = "EX.EXAMPLE")
    public abstract class Entity
    {
      private CustomEntity customEntity;
      public static final String CUSTOM_ENTITY = "customEntity";

      private CustomEntity customEntity2;
      public static final String CUSTOM_ENTITY2 = "customEntity2";


      @OneToOne(cascade = CascadeType.ALL, mappedBy = CustomEntity.ENTITY, fetch = FetchType.LAZY)
      @Fetch(FetchMode.SELECT)
      public CustomEntity getCustomEntity()
      {
        return this.customEntity;
      }

      @OneToOne(cascade = CascadeType.ALL, mappedBy = CustomEntity.ENTITY, fetch = FetchType.LAZY)
      @Fetch(FetchMode.SELECT)
      public CustomEntity getCustomEntity2()
      {
        return this.customEntity2;
      }
    }

我只是通过将 customEntity 更改为 Entity 中的列表来管理它。

问候

【问题讨论】:

  • 两者都映射到CustomEntity中的同一个字段?
  • 是的,你可以这样做。您对此有什么问题吗?
  • 它们都映射到同一个实体,这就是为什么我不知道如何管理它。
  • 你可以这样做,你只需要在CustomEntity中使用两个连接列和两个字段

标签: java hibernate mapping conditional


【解决方案1】:

是的,这是完全正常的情况。您只需要两个具有不同 mappedBy 的字段,每个关系一个字段

  @OneToOne(cascade = CascadeType.ALL, mappedBy = CustomEntity.ENTITY1, fetch = FetchType.LAZY)
  @Fetch(FetchMode.SELECT)
  public CustomEntity getCustomEntity()
  {
    return this.customEntity;
  }

  @OneToOne(cascade = CascadeType.ALL, mappedBy = CustomEntity.ENTITY2, fetch = FetchType.LAZY)
  @Fetch(FetchMode.SELECT)
  @JoinColumn(name = "entity_2_id")
  public CustomEntity getCustomEntity2()
  {
    return this.customEntity2;
  }

还有CustomEntity中的两个字段,每个映射一个

  @OneToOne
  @JoinColumn(name = "ID_ENTITY_1", nullable = true)
  public Entity getEntity1()
  {
    return this.entity1;
  }

  @OneToOne
  @JoinColumn(name = "ID_ENTITY_2", nullable = true)
  public Entity getEntity2()
  {
    return this.entity2;
  }

【讨论】:

  • 但这意味着我必须在我的自定义实体表中添加另一列引用相同的 id
  • 不必引用同一个id。使用相同的 ID,您的问题中的 getCustomEntity()getCustomEntity2() 将返回相同的对象。据我了解,您希望在EntityCustomEntity 之间有两种不同的关系。你能澄清一下你的要求吗?
  • 好的,我试试。 1 个实体可以有一个 CustomEntity 列表,通常应该是 OneToMany 关系。但在我的情况下,总是最多只有两个 CustomEntity,所以我不想为这种情况创建一个列表。
  • 然后将Entity 设为关系的所有者,并将@JoinColumns 移动到映射的一侧。连接列将包含两个相关 CustomEntitys 的 id。
  • 是的,谢谢。我心里也有这个办法。我希望有另一种方法来避免数据库更改。但我认为这是无法避免的。再次感谢您!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
  • 2014-06-13
相关资源
最近更新 更多