假设我设置了三个表(不是我的设计):
表'Foo'& '酒吧':
---------- ----------
| 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 |
------------------------------------------------------------
好的,所以这个'annotaion'表实际上是按名称和主键引用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
}