Reputation: 719
I have two table A and B
Table A:
ID_A
name
table B
ID_B
name
I joined both by a third table C table with their primary key
table C
ID_C
ID_A
ID_B
I'd like to know this relationship in jpa mapping to retrieve the list of object B inside object A
thank you,
Upvotes: 0
Views: 2663
Reputation: 719
I have find a good example here http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/
Upvotes: 0
Reputation: 2931
Class A has list of C objects.
class A{
@Id
private Long Id;
@Column(name = "name_a", length = 5)
private Strin name_a;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "a", fetch = FetchType.LAZY)
private List<C> cList;
}
class B{
@Id
private Long Id;
@Column(name = "name_b", length = 5)
private String name_b;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "b", fetch = FetchType.LAZY)
private List<C> cList;
}
This is join table.Class C has A object and B object.
class C{
@Id
private Long id;
@JoinColumn(name = "id_a", referencedColumnName = "id", nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private A a;
@JoinColumn(name = "id_b", referencedColumnName = "id", nullable = false)
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private B b;
}
Upvotes: 1