我确实遇到了一种情况,我需要根据某些条件从三个不同的表中获取数据。 例如。
@Entity
class Order{
id;
type;
typeId;
@Join with boqTable to get the Boq Associated with if type == BOQ and id = typeId
@Join with customTable to get custom data associated if type == CUSTOM and id = typeId
}
我应该能够使用 Order 实体插入和更新子表。 我如何使用 JPA/Hibernate 来实现这一点。我搜索了很多,除了在服务级别手动执行而不是在实体模型处理之外,我找不到任何解决方案
最佳答案
您可以使用 2 个不符合 JPA 的 Hibernate 特定功能之一。如果条件是静态的,则查看 Hibernate @Where
注释。请参阅第 2.4.6 节:
https://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/
否则请参阅 Hibernate 过滤器:
Hibernate3 has the ability to pre-define filter criteria and attach those filters at both a class level and a collection level. A filter criteria allows you to define a restriction clause similar to the existing "where" attribute available on the class and various collection elements. These filter conditions, however, can be parameterized. The application can then decide at runtime whether certain filters should be enabled and what their parameter values should be. Filters can be used like database views, but they are parameterized inside the application.
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/filters.html
关于java - 实体 JPA/Hibernate 中的条件 JoinColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38996774/