【问题标题】:Composite Primary Key JPA representation复合主键 JPA 表示
【发布时间】:2018-01-26 15:31:16
【问题描述】:

我很难在休眠中映射复合主键连接。这是我的架构的简化图片。请注意,我无法更改架构。

Simplified Schema

这是类的编码方式。

树类

@Entity
@Table(name = "tree")
public class Tree implements Serializable {
   @EmbeddedId
   protected TreePK treePK;

  @OneToOne(mappedBy = "tree")
  private Activity activity;
}

树PK类

@Embeddable
public class TreePK implements Serializable {
    @Column(name = "id")
    private Integer id;

    //this is a foreign key from the volume table
    @Column(name = "volume_id")
    private Integer volumeId;
}

卷类:

@Entity
@Table(name = "volume")
public class Volume implements Serializable {
   @Id
   private Integer id;
}

活动类:

@Entity
@Table(name = "activity")
public class Activity implements Serializable {
   @Id
   private Integer id;

   @JoinColumn(name = "tree_id", referencedColumnName = "id")    
   @OneToOne
   private Tree tree;
}

对于活动类: 树有一个复合主键。 Activity 在 Tree 的复合主键中的“一个”列 (id) 上有一个外键。 Activity 类中的以下行

@JoinColumn(name = "tree_id", referencedColumnName = "id")

抛出异常

com.corp.Activity.tree 的referencedColumnNames(id) 引用 com.corp.Tree 未映射到单个属性

如何在 Hibernate 中建立这样的关系?尽管主键是复合的,但我必须映射到单个属性 (id)。

【问题讨论】:

  • 顺便说一句,你确定这种方法是一致的吗?如果你的主键是复合的——那么任何东西怎么能只通过一列复合 id 来引用 SINGLE 实体呢?如果在现实中这是可能的——那么这个单列就是 REAl 主键,你可以在 hibernate 中使用它,

标签: hibernate jpa


【解决方案1】:

Eclipselink 和 Hibernate 都不能做到这一点。许多年前,OpenJPA 是唯一可以做到这一点的 JPA 实现。

【讨论】:

  • 感谢您的信息。我将尝试使用 OpenJPA 作为提供程序而不是 Hibernate。
  • @b0ri 拜托,试试看它是否有效 - 让我知道。
猜你喜欢
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2013-06-07
  • 2019-04-22
  • 1970-01-01
  • 2023-03-07
  • 2021-10-07
相关资源
最近更新 更多