Javier Ferreiro
Javier Ferreiro

Reputation: 21

How to make a foreign key in EmbeddedId to other Entity with other EmbeddedId

I have the class SWEntity that is central to the problem. I want set of classes SWEntityRow that is a detail of the SWEntity (one entity has many rows). The SWentity has in the key two other classes in the embeddedId Area and Procedure. When I try to map SWEntity with SWEntityRow using SWEntityRowId I take differents errors.

Some idea how I can map SWEntityRowId with SWEntity?

This is a simplified picture of the ER: https://i.sstatic.net/pMJzh.png

And this are my simplified classes:

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"
    })
}

Upvotes: 1

Views: 1155

Answers (2)

Javier Ferreiro
Javier Ferreiro

Reputation: 21

This resolve my question

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

The difference is in referencedColumnName="AreaName" not referencedColumnName="area_name" and the same for procedure.

Thanks!

Upvotes: 1

Rajeev Ranjan
Rajeev Ranjan

Reputation: 279

Just one change required

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

Upvotes: 0

Related Questions