如何在EmbeddedId中将外键指向具有其他EmbeddedId的其他实体

时间:2019-06-12 11:32:52

标签: java spring-boot jpa

我有一个对问题至关重要的SWEntity类。我想要一组类SWEntityRow,它是SWEntity的详细信息(一个实体有很多行)。 SWentity在关键的另外两个类别中的EmbeddedId Area和Procedure。当我尝试使用SWEntityRowId将SWEntity与SWEntityRow映射时,会出现差异错误。

一些想法如何将SWEntityRowId与SWEntity映射?

这是ER的简化图:https://i.stack.imgur.com/pMJzh.png

这是我的简化课程:

SWentity
-----------
@Entity
public class SWEntity {

    @EmbeddedId
    private SWEntityId id;
    [...]
}


SWEntityId
-------------
@Embeddable
public class SWEntityId implements Serializable{
    private static final long serialVersionUID = 1L;
    @NotNull
    private String name;
    @NotNull
    private int version;

    @ManyToOne
    @JoinColumn(name = "areaName", nullable = false)
    @JsonIgnore
    private Area area;

    @ManyToOne
    @JoinColumn(name = "procedureName", nullable = false)
    @JsonIgnore
    private Procedure procedure;
}


SWEntityRow
---------------
@Entity
public class SWEntityRow{

    @EmbeddedId
    private SWEntityRowId sWEntityRowId;
}

SWEntityRowId
---------------

@Embeddable
public class SWEntityRowId implements Serializable {

    private static final long serialVersionUID = 1L;

    private String rowName;

    //SWEntityId
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="name_row", referencedColumnName="name"),
        @JoinColumn(name="version_row", referencedColumnName="version"),
        @JoinColumn(name="area_row", referencedColumnName="area_name"),
        @JoinColumn(name="procedure_row", referencedColumnName="procedure_name"
    })
}

2 个答案:

答案 0 :(得分:1)

只需更改一次

@Entity 
public class SWEntity {
  @Id
  @EmbeddedId
  private SWEntityId id;
  [...]
}

答案 1 :(得分:1)

这解决了我的问题

@ JoinColumns({
        @JoinColumn(name="entityname", referencedColumnName="name"),
        @JoinColumn(name="entityversion", referencedColumnName="version"),
        @JoinColumn(name="entityarea", referencedColumnName="AreaName"),
        @JoinColumn(name="entityprocedure", referencedColumnName="ProcedureName")
    })

区别在于referencedColumnName =“ AreaName”不是referencedColumnName =“ area_name”,而过程相同。

谢谢!