-1

I have two tables A and B. There are two columns in table A, col1 and col2(both the columns are primary key, i.e. composite with col1 and col2). There is one column in table B, to which both the columns from table A are pointing to, i.e. col1 and col2 are foreign keys with relation to column in table B.

How can I implement the JPA entity for table A?

Thank you

1 Answers1

1

Well you can achieve that by following code:

@Embeddable
public class AID {
    public int xID;
    public int yId;
}

@Entity
public class A {
    @EmbeddedId
    public AID id;

    @OneToMany(mappedBy="A")
    public Collection<B> b;
}


@Entity
public class Meeting {
    @ID
    @GeneratedValue
    public Long id;

    @MapsId("aID")
    @JoinColumns({
        @JoinColumn(name="xID", referencedColumnName="xID"),
        @JoinColumn(name="yId", referencedColumnName="yId")
    })
    @ManyToOne
    public A a;
}
Jayesh Choudhary
  • 748
  • 2
  • 12
  • 30