可以使用`@JoinColumn`来映射一对多关系,而无需外键和关联实体吗?

huangapple go评论94阅读模式
英文:

Is possible to use JoinColumn to map OneToMany relation without ForeignKey and Association Entity?

问题

我在思考如何映射这样的关系:

@Table(name = "master_a")
class MasterA {
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Where(clause = "type = 'MASTER_A'")
    Set<Child> childs;
}

@Table(name = "master_b")
class MasterB {
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Where(clause = "type = 'MASTER_B'")
    Set<Child> childs;
}

@Table(name = "child")
class Child {
    Long id;
    String type;
    Long masterId;
}

Child表格:

Id Type Master_Id
1 MASTER_B 1
2 MASTER_A 1
3 MASTER_B 1

Child表格只有PK和索引的type/master_id字段(没有外键)。

对于常量值,我使用了Hibernate的@Where条件,但如何映射master_id变量呢?
我尝试在OneToMany中使用以下方式:

@JoinColumn(
    foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),
    name = "id",
    referencedColumnName = "master_id"
)

但Hibernate忽略了ForeignKey注解,仍然在数据库中寻找外键。我找到了"DiscriminatorColumn"注解,可以像这个问题中使用:

当使用@DiscriminatorValue

但在我的情况下,Child实体不是一个超类 - 是否可以在另一个方向上使用这个机制?

Child实体包含可选的额外数据,用于X实体(我想将这些数据存储在一个数据库表中),并让Hibernate为不同的主类型执行子查询,例如:

select c.* from child c where c.type='CONSTANT_MASTER_TYPE_BASE_ON_CLASS_TYPE' and c.master_id=?.id
英文:

I'm wondering how to map such a relationship:

@Table(&quot;master_a&quot;)
class MasterA{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Where(clause = &quot;type = &#39;MASTER_A&#39;&quot;)
Set&lt;Child&gt; childs;
}

@Table(&quot;master_b&quot;)
class MasterB{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Where(clause = &quot;type = &#39;MASTER_B&#39;&quot;)
Set&lt;Child&gt; childs;
}

@Table(&quot;child&quot;)
class Child{
Long id;
String type;
Long masterId;
}

Table Child:

| Id | Type | Master_Id |

| --- | --- | --- |

| 1 | MASTER_B | 1 |

| 2 | MASTER_A | 1 |

| 3 | MASTER_B | 1 |

Child table has only PK and indexed type/master_id fields (without foreign keys).

For constant values I used hibernate @Where condition, but how to map master_id variable?
I tried in OneToMany:

@JoinColumn(foreignKey = @ForeignKey(name = &quot;none&quot;, value = ConstraintMode.NO_CONSTRAINT),name = &quot;id&quot;, referencedColumnName = &quot;master_id&quot;)

But Hibernate ignoring ForeignKey annotation and still search for database FK.
I found "DiscriminatorColumn" annotation which can be used like in this question:
When to use @DiscriminatorValue

But in my case Child entity is not a superclass - can this mechanism be used in other direction?

Child entity holds optional additional data for X enitities (I want store this data in one db table) and make Hibernate do subqueries for different masters types like:
select c.* from child c where c.type='CONSTANT_MASTER_TYPE_BASE_ON_CLASS_TYPE' and c.master_id=?.id

答案1

得分: 0

为什么在你的连接列中定义一个外键,如果你不想要一个的话?不要添加它。

你可能只需要在子表的"child.masterId" 上创建索引,因为它将在主表的检索中使用,如果你是从子表的角度来控制这个列,可以将连接列的"insertable=false, updatable=false"。

英文:

Why are you defining a foreign key in your join column if you don't want one? Leave it off.

All you will likely need is indexing on the child.masterId as it will be used on the Master fetches, and if you are controlling the column from the Child side, make the join column insertable=false, updatable=false

@JoinColumn(name = &quot;id&quot;, referencedColumnName = &quot;master_id&quot;, insertable=false, updatable=false)

huangapple
  • 本文由 发表于 2023年7月13日 16:13:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定