假设我像这样设置了三个表(不是我的设计):
表“Foo”和“Bar”:
---------- ----------
| FOO_ID | | BAR_ID |
---------- ----------
| 1 | | 1 |
| 2 | | 3 |
---------- ----------
*这里没什么特别的,它们只是两个带 id 的表
表“注释”:
------------------------------------------------------------
| ANNOTATION_ID | ANNOTATION_TEXT | OTHER_TABLE | OTHER_ID |
------------------------------------------------------------
| 1 | 'annotation a' | 'foo' | 1 |
| 2 | 'annotation b' | 'foo' | 2 |
| 3 | 'annotation c' | 'baz' | 1 |
| 4 | 'annotation d' | 'baz' | 3 |
------------------------------------------------------------
好的,所以这个“注释”表实际上是按名称和主键引用 foo 和 baz 表。这里绝对没有参照完整性。这不是我的设计。
抛开免责声明不谈,在 SQL 中join
会是这样的:
select *
from foo
left join annotation
on (other_table = 'foo' and other_id = foo_id)
我想使用带有注释的 Hibernate 映射这种关系。恐怕这是不可能的,但想问,你永远不知道。我的Annotation
课看起来像这样:
@Entity
@Table(name = "ANNOTATION")
public class Annotation {
@Id
@Column(name = "ANNOTATION_ID")
private Integer id;
@Column(name = "ANNOTATION_text")
private String text;
@Column(name = "OTHER_TABLE")
private Integer other_table;
@Column(name = "OTHER_ID")
private Integer other_id;
// getters & setters
}
任何想法如何在 Hibernate 中设置这个一对多(Foo
一对多Annotations
)映射?
@Entity
@Table(name = "FOO")
public class Foo {
@Id
@Column(name = "FOO_ID")
private Integer id;
@OneToMany
@Column // how would I configure this?
private Collection<Annotation> annotations;
// getters & setters
}