英文:
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"注解,可以像这个问题中使用:
但在我的情况下,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("master_a")
class MasterA{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Where(clause = "type = 'MASTER_A'")
Set<Child> childs;
}
@Table("master_b")
class MasterB{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Where(clause = "type = 'MASTER_B'")
Set<Child> childs;
}
@Table("child")
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 = "none", value = ConstraintMode.NO_CONSTRAINT),name = "id", referencedColumnName = "master_id")
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 = "id", referencedColumnName = "master_id", insertable=false, updatable=false)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论