0

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,

user820688
  • 719
  • 2
  • 13
  • 27

2 Answers2

1

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;


}
mstzn
  • 2,881
  • 3
  • 25
  • 37
  • Thanks for response, this is correct but what i want us to have sommethong like private List bList; inside object A, is this possible, wow to do to map this ? – user820688 Oct 09 '12 at 15:37
  • it is imposible that you want because of relationship between A and B.And I think it is not necessary – mstzn Oct 09 '12 at 17:31
0

I have find a good example here http://viralpatel.net/blogs/hibernate-many-to-many-annotation-mapping-tutorial/

user820688
  • 719
  • 2
  • 13
  • 27