在OneToMany关系中使用javax.persistence加入3个表

时间:2016-05-11 01:29:25

标签: java sql hibernate jpa javax.persistence

这3个表是“分析组”,“labinstructions”,“观察到的属性”。每个表都有一个“id”主键列。

我想使用第4个表(“analyticalgroups_observedproperties_labinstructions”)来存储OneToMany关系。最终我希望输出结构如下:

analyticalGroup: {
  id: "...",
  observedPropertyLabInstructions: [
    {observedProperty, labInstruction},
    {observedProperty, labInstruction},
    {observedProperty, labInstruction},
    ...etc...
  ]
}

我已经在网上关注了一些例子,但无法让它发挥作用。问题是,当我尝试这个时,我收到以下错误:

  

“message”:“存储库发生错误:PSQLException:错误:列observepr0_.observedpropertyentitylabinstructionentitymap_id不存在\ n位置:6550”,     “errorCode”:“gaia.domain.exceptions.RepositoryException”,

这是连接表的结构。

CREATE TABLE analyticalgroups_observedproperties_labinstructions
(
  analyticalgroupid character varying(36) NOT NULL,
  labinstructionid character varying(36) NOT NULL,
  observedpropertyid character varying(36) NOT NULL,
  CONSTRAINT fk_analyticalgroups_observedproperties_labinstructions_groupid FOREIGN KEY (analyticalgroupid)
      REFERENCES analyticalgroups (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT fk_analyticalgroups_observedproperties_labinstructions_labinstr FOREIGN KEY (labinstructionid)
      REFERENCES labinstructions (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE,
  CONSTRAINT fk_analyticalgroups_observedproperties_labinstructions_observed FOREIGN KEY (observedpropertyid)
      REFERENCES observedproperties (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE CASCADE
)
@Entity
@Data
public class AnalyticalGroupEntity {

    public static final String ENTITY_NAME = "analyticalGroups";
    public static final String JOIN_OBSERVEDPROPERTIES_LABINSTRUCTIONS_TABLE_NAME =
            ENTITY_NAME +
                    IDomainEntity.UNDERSCORE +
                    ObservedPropertyEntity.ENTITY_NAME +
                    IDomainEntity.UNDERSCORE +
                    LabInstructionEntity.ENTITY_NAME;

    @Id
    @Column(name = IDomainEntity.ID_KEY, nullable = false, columnDefinition = IDomainEntity.COLUMN_TYPE_UUID)
    private String id;

    @OneToMany
    @JoinTable(
            name = JOIN_OBSERVEDPROPERTIES_LABINSTRUCTIONS_TABLE_NAME,
            joinColumns = @JoinColumn(name = LabInstructionEntity.ID_KEY, referencedColumnName = IDomainEntity.ID_KEY, table = "labinstructions")
    )
    @MapKeyJoinColumn(name = ObservedPropertyEntity.ID_KEY, referencedColumnName = IDomainEntity.ID_KEY, table = "observedproperties")
    private Map<ObservedPropertyEntity, LabInstructionEntity> observedPropertyLabInstructions;

}

希望我已经尽可能清楚地表达了这一切。 非常感谢您的帮助。谢谢你的阅读!

1 个答案:

答案 0 :(得分:0)

编辑实际上......事实证明这并不奏效。它成功获取了我想要的数据,每当我发出GET请求时,buuuuut也会删除连接表中的每一行*翻转表*

太奇怪了!

@OneToMany
@JoinTable(
        name = JOIN_OBSERVEDPROPERTIES_LABINSTRUCTIONS_TABLE_NAME,
        joinColumns = @JoinColumn(name = "analyticalgroupid", referencedColumnName = IDomainEntity.ID_KEY, table = "labinstructions"),
        inverseJoinColumns = @JoinColumn(name = LabInstructionEntity.ID_KEY, referencedColumnName = IDomainEntity.ID_KEY, table = "labinstructions")
)
@MapKeyJoinColumn(name = ObservedPropertyEntity.ID_KEY, referencedColumnName = IDomainEntity.ID_KEY, table = "observedproperties")
private Map<ObservedPropertyEntity, LabInstructionEntity> observedPropertyEntityLabInstructionEntityMap;