如何运作Hibernate @ManyToMany关系?

时间:2014-03-17 13:44:26

标签: java hibernate jpa orm jpa-2.0

我有3个实体和一个实体用于链接这三个实体:

@Entity
public class A {

          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "a", fetch = FetchType.LAZY)
    private List<ABCXref> abcXref = new ArrayList<ABCXref>();

          // getters/setters
}

@Entity
public class B {

          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "b", fetch = FetchType.LAZY)
    private List<ABCXref> abcXref = new ArrayList<ABCXref>();

          // getters/setters
}

@Entity
public class C {

          @Id
          @GeneratedValue(strategy = GenerationType.AUTO)
          private Long id;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "c", fetch = FetchType.LAZY)
    private List<ABCXref> abcXref = new ArrayList<ABCXref>();

          // getters/setters
}


@Entity
public class ABCXref {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JoinColumn(name = "A_ID", nullable = true)
    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    private A a;

    @JoinColumn(name = "B_ID")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)  
    private B b;  

    @JoinColumn(name = "C_ID")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private C c;
}

问题是:A,B和C的所有实例是否共享ABCXref's的相同列表?如果我向ABCXref实例添加新A该怎么办?此ABCXref是否出现在ABCXrefsB的{​​{1}}列表中?

即。这段代码会输出C吗?

true

0 个答案:

没有答案