在Hibernate映射OneToMany关系的路上

时间:2023-02-07 15:25:49

Let's take as example two entities A and B. Each A may have many associated Bs. Each B is associated to exactly one A. I represent this relationship with Hibernate as follows:

让我们以两个实体A和B为例。每个A可以有许多相关的B。每个B只与一个A相关联。我用Hibernate表示这种关系,如下所示:

@Entity
public class A {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "AId")
    private Long id;

    @Column(name = "AName", nullable = false, unique = true)
    private String name;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "A_B", joinColumns = { @JoinColumn(name = "AId") }, inverseJoinColumns = { @JoinColumn(name = "BId") })
    private Set<B> bs = new HashSet<B>();

}


@Entity
public class B {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "BId")
    private Long id;

    @Column(name = "BName")
    private String name;

    @ManyToOne(optional = false)
    @JoinColumn(name = "BA")
    private A a;

}

When I generate the DB from this mapping, here's what I get:

当我从这个映射生成数据库时,这是我得到的:

  • Table A.
  • The join table A_B with 2 columns: AId and BId. AId and BId belong to the primary key. And since there is a OneToMany association from A to B (i.e. one A may have many associated Bs, and one B is associated to exactly one A), a unique constraint is added on BId.
  • 连接表A_B有2列:AId和BId。 AId和BId属于主键。并且由于存在从A到B的OneToMany关联(即,一个A可能具有许多关联的B,并且一个B恰好与一个A关联),所以在BId上添加唯一约束。

  • Table B with columns including BA which is a foreign key referencing column AId of table A.
  • 表B包含BA,其中BA是表A的外键引用列AId。

Concerning the join table,

关于连接表,

  • why does Hibernate map the OneToMany relationship this way?
  • 为什么Hibernate以这种方式映射OneToMany关系?

  • It could simply define the primary key as consisting of one column (BId) and the same rules would be enforced.
  • 它可以简单地将主键定义为由一列(BId)组成,并且将强制执行相同的规则。

  • Is there a reason why the mapping is done this way?
  • 有没有理由以这种方式完成映射?

  • What are the advantages/drawbacks of such a mapping?
  • 这种映射有哪些优点/缺点?

I precise I'm using Hibernate 4.1.6.Final.

我确切地说我正在使用Hibernate 4.1.6.Final。

1 个解决方案

#1


0  

I see that you are trying to map a bidirectional association: from A to B and from B to A.

我看到你试图映射双向关联:从A到B,从B到A.

Concerning the join table, why does Hibernate map the OneToMany relationship this way?

关于连接表,为什么Hibernate会以这种方式映射OneToMany关系?

Because you specified a join table. You can map a @OneToMany relation without it:

因为您指定了连接表。您可以在没有它的情况下映射@OneToMany关系:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "AId", nullable = false)
private Set<B> bs = new HashSet<B>();

or better as you have a bidirectional association:

或者更好,因为你有一个双向关联:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "a")  
private Set<B> bs = new HashSet<B>();

...

@ManyToOne(optional = false)  
@JoinColumn(name = "AId", nullable = false)  
private A a;

Regarding

Is there a reason why the mapping is done this way? What are the advantages/drawbacks of such a mapping?

有没有理由以这种方式完成映射?这种映射有哪些优点/缺点?

The mapping with the join table is useful when you have an optional one to many, if, for example B could have no A. It allows you to avoid a nullable AId column on the B table.

如果您有一个可选的一对多,那么使用连接表的映射非常有用,例如,如果B可能没有A.它允许您避免B表上可以为空的AId列。

#1


0  

I see that you are trying to map a bidirectional association: from A to B and from B to A.

我看到你试图映射双向关联:从A到B,从B到A.

Concerning the join table, why does Hibernate map the OneToMany relationship this way?

关于连接表,为什么Hibernate会以这种方式映射OneToMany关系?

Because you specified a join table. You can map a @OneToMany relation without it:

因为您指定了连接表。您可以在没有它的情况下映射@OneToMany关系:

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "AId", nullable = false)
private Set<B> bs = new HashSet<B>();

or better as you have a bidirectional association:

或者更好,因为你有一个双向关联:

@OneToMany(cascade = CascadeType.ALL, mappedBy = "a")  
private Set<B> bs = new HashSet<B>();

...

@ManyToOne(optional = false)  
@JoinColumn(name = "AId", nullable = false)  
private A a;

Regarding

Is there a reason why the mapping is done this way? What are the advantages/drawbacks of such a mapping?

有没有理由以这种方式完成映射?这种映射有哪些优点/缺点?

The mapping with the join table is useful when you have an optional one to many, if, for example B could have no A. It allows you to avoid a nullable AId column on the B table.

如果您有一个可选的一对多,那么使用连接表的映射非常有用,例如,如果B可能没有A.它允许您避免B表上可以为空的AId列。