请让我展示代码,
@Entity
class R {
@Id
Long id;
String selector;
long fkey;
// other columns...
}
@Entity
class Foo {
@Id
Long id;
// select * from R where selector='A' and fkey=Foo_id
@OneToMany
Set<R> aSet;
// select * from R where selector='B' and fkey=Foo_id
@OneToMany
Set<R> bSet;
}
在这里,我无法将 R
拆分为两个表:R_A
和 R_B
,因为选择器是变体的。
我知道我可以创建 View R_A 和 R_B,但我不知道如何让 Hibernate 为 View 生成 DDL。或者也许我应该在实体注释中指定自定义 SQL 查询?就像,
@Entity
@SourceSQL("select * from R where selector='A')
class R_A { ... }
或者也许是这样的,
@Entity
class Foo {
@Id
Long id;
@OneToMany
@RestrictJoin("selector = 'A'")
Set<R> aSet;
@OneToMany
@RestrictJoin("selector = 'B'")
Set<R> bSet;
}
好的,提前致谢。
最佳答案
一个选项是@Filter注释,看看:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-hibspec-filters 。这是关于如何使用它们的一个很好的示例:http://java.dzone.com/articles/introduction-hibernate-filters .
过滤器不需要有参数,请查看另一个示例: https://forum.hibernate.org/viewtopic.php?f=1&t=996694&start=0 .
请注意,您需要为每个 session 启用过滤器,您可以使用过滤器来执行此操作:http://forum.springsource.org/showthread.php?t=61464 .
如果您决定每个选择器使用一个类,您可能需要考虑继承和 @DiscriminatorColumn 注释,例如: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html#d0e1168
关于java - Hibernate:如何将R(选择器,键,*)中的不同选择器映射到不同字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5590196/