ManyToMany关系和ManyToMany与另一个类

时间:2016-10-16 15:10:04

标签: java hibernate jpa orm

经过大量搜索没有运气后,我决定在这里提出我的问题,所以我首先举一个例子来说清楚。

我有4个班级A - B - C - D. 我在A和B之间有一个名为A_B的ManyToMany关系(我知道如何使用@idClass或@Embeddable和@embeddedId编写代码)

现在我需要的是在A_B和C之间建立一个ManyToMany关系,所以我将有另一个名为A_B_C的连接表

A_B_C类也将与D的ManyToMany关系,我将得到一个A_B_C_D连接表。

如果有人能帮我解决这个问题,我将不胜感激。

2 个答案:

答案 0 :(得分:2)

使用JPA复合pks的一个简单示例可能是:

@Entity
class A {
  @Id
  Integer id;
  @OneToMany(mappedBy="a")
  List<AB> abList;
}

@Entity
class B {
  @Id
  Integer id;
  @OneToMany(mappedBy="b")
  List<AB> abList;
}

@Entity
@IdClass(ABId.class)
class AB {
  @Id
  @ManyToOne
  public A a;
  @Id
  @ManyToOne
  public B b;
  @OneToMany(mappedBy="ab")
  List<ABC> abcList;
}

class ABId {
  public Integer a;
  public Integer b;
}

@Entity
@IdClass(ABCId.class)
class ABC {
  @Id
  @ManyToOne
  public AB ab;
  @Id
  @ManyToOne
  public C c;
  @ManyToMany
  List<D> dList;
}

class ABCId {
  public ABId ab;
  public Integer c;
}

答案 1 :(得分:0)

基于@Chris给出的答案,这是我的功能代码。

A类:

@Entity
@Table(name = "A")
public class A implements Serializable {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private Long idA;
private Set<A_B> a_bs = new HashSet<A_B>();

@Id
@GeneratedValue
@Column(name = "A_ID")
public Long getIdA() {
    return idA;
}
public void setIdA(Long idA) {
    this.idA = idA;
}

@OneToMany(mappedBy = "a", cascade = CascadeType.ALL)
public Set<A_B> getA_bs() {
    return a_bs;
}
public void setA_bs(Set<A_B> a_bs) {
    this.a_bs = a_bs;
}

public void addA_B(A_B a_b){

    this.a_bs.add(a_b);
}

public A() {
    super();
}
}

B类:

@Entity
@Table(name = "B")
public class Matiere implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Long idB;
private Set<A_B> a_bs = new HashSet<A_B>();

@Id
@GeneratedValue
@Column(name = "B_ID")
public Long getIdB() {
    return idB;
}

public void setIdB(Long idB) {
    this.idB= idB;
}

@OneToMany(mappedBy = "b", cascade = CascadeType.ALL)
public Set<A_B> getA_bs() {
    return a_bs;
}

public void setA_bs(Set<A_B> a_bs) {
    this.a_bs = a_bs;
}

public void addA_B(A_B a_b){

    this.a_bs.add(a_b);
}

public Matiere() {
    super();
    // TODO Auto-generated constructor stub
}
}

A_B类:

@Entity
@Table(name="A_B")
@IdClass(A_BId.class)
public class A_B implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;


private A a;
private B b;
private Set<A_B_C> a_b_cs = new HashSet<A_B_C>();

@Id
@ManyToOne
@JoinColumn(name="A_ID")
public A getA() {
    return a;
}
public void setA(A a) {
    this.a= a;
}

@Id
@ManyToOne
@JoinColumn(name="B_ID")
public B getB() {
    return b;
}
public void setB(B b) {
    this.b= b;
}

@OneToMany(mappedBy="a_b", cascade = CascadeType.ALL)
public Set<A_B_C> getA_b_cs() {
    return a_b_cs;
}
public void setA_b_cs(Set<A_B_C> A_b_cs) {
    this.a_b_cs = a_b_cs;
}
}

A_BId类:

public class A_BId implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

private Long  a;
private Long b;
public Long getA() {
    return a;
}
public void setA(Long a) {
    this.a = a;
}
public Long getB() {
    return b;
}
public void setB(Long b) {
    this.b = b;
}
@Override
public int hashCode() {
    // Code generated here
}
@Override
public boolean equals(Object obj) {
    // Code generated here 
}

C类:

@Entity
@Table(name = "C")
public class C implements Serializable {

private static final long serialVersionUID = 1L;
private Long idC;
private Set<A_B_C> a_b_cs = new HashSet<A_B_C>();

@Id
@GeneratedValue
@Column(name = "C_ID")
public Long getIdC() {
    return idC;
}

public void setIdC(Long idC) {
    this.idC= idC;
}

@OneToMany(mappedBy="c", cascade = CascadeType.ALL)
public Set<A_B_C> getA_b_cs() {
    return a_b_cs;
}

public void setA_b_cs(Set<A_B_C> a_b_cs) {
    this.a_b_cs = a_b_cs;
}

public void addABC(A_B_C a_b_c){
    this.a_b_cs.add(a_b_c);
}

public Niveau() {
    super();
}
}

A_B_C类:

@Entity
@Table(name="A_B_C")
@IdClass(A_B_CId.class)
public class A_B_C implements Serializable{

private static final long serialVersionUID = 1L;
private A_B a_b;
private C c;

@Id
@ManyToOne
@JoinColumns({
    @JoinColumn(name="A_ID", referencedColumnName="A_ID"),
    @JoinColumn(name="B_ID", referencedColumnName="B_ID")
})
public A_B getA_b() {
    return a_b;
}

public void setA_b(A_B a_b) {
    this.a_b = a_b;
}

@Id
@ManyToOne
@JoinColumns({
    @JoinColumn(name="C_ID", referencedColumnName="C_ID")
})
public C getC() {
    return c;
}

public void setC(C c) {
    this.c= c;
}
}

A_B_CId类:

public class A_B_CId implements Serializable {

private static final long serialVersionUID = 1L;
private A_BId a_b;
private Long c;

public A_BId getA_b() {
    return a_b;
}
public void setA_b(A_BId a_b) {
    this.a_b = a_b;
}
public Long getC() {
    return c;
}
public void setC(Long c) {
    this.c= c;
}
public A_B_CId() {
    super();
    // TODO Auto-generated constructor stub
}
@Override
public int hashCode() {
    // Code generated
}
@Override
public boolean equals(Object obj) {
    // Code generated
}