Reputation: 273
I have two tables that share a common key. What I'd like is that when I load the class for the first table, I would also get a list of classes corresponding to the second table where they share the key.
More specifically: tableA has an id (id1) that exists in multiple entries of tableB. However, tableB uses a composite key and uses both id1 and id2 as keys.
What I'd like to achieve is that when the POJO for tableA is loaded, i also get all entries of table B where id1 from table A equals id1 in table B. Something like:
public class TableA {
private String id1;
private List<TableB> list; // where id1 in tableA == id1 in tableB
}
table A id column:
id1
table B composite id:
id1 // same as for tableA
id2
Upvotes: 0
Views: 49
Reputation: 23226
Class B will have a composite Identifier partially dependent on A. This can be mapped using an EmbeddedID
or by specifiyng an ID class. Using an ID class would look like the below:
Entity A
@Entity
public class A {
@Id
@Column(name = "id1")
private Long id1;
@OneToMany(mappedBy = "a")
private Set<B> bs;
}
Entity B
@Entity
@IdClass(Bid.class)
public class B {
@Id
@ManyToOne
@JoinColumn(name = "id1")
private A a;
@Id
@Column(name = "id1")
private Long id2;
}
ID Class for B
public class Bid implements Serializable{
//must be of same type as id of target entity A
private Long a;
private Long id2;
// **must** implement equals and hash code
}
Upvotes: 1