hibernate jpa用另一个表连接两个表

时间:2012-10-09 15:07:06

标签: jpa-2.0 hibernate-mapping hibernate-onetomany

我有两张表A和B

表A:

ID_A

名称

表B

ID_B

名称

我通过第三个表C表和他们的主键

加入了

表C

ID_C

ID_A

ID_B

我想知道jpa映射中的这种关系,以检索对象A中对象B的列表

谢谢,

2 个答案:

答案 0 :(得分:1)

A类包含C对象列表。

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;

}

这是连接表.Class C有一个对象和B对象。

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;


}

答案 1 :(得分:0)