【问题标题】:Conditional JoinColumn in Entity JPA/Hibernate实体 JPA/Hibernate 中的条件 JoinColumn
【发布时间】:2016-08-17 12:32:30
【问题描述】:

我确实有一种情况,我需要根据某些条件从三个不同的表中获取数据。 例如。

@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.. 我搜索了很多,除了在服务级别手动执行而不是在实体模型处理之外,我找不到任何解决方案

【问题讨论】:

  • 没有条件连接。

标签: java spring hibernate jpa


【解决方案1】:

您可以使用 2 个不符合 JPA 的 Hibernate 特定功能之一。如果条件是静态的,请查看 Hibernate @Where 注释。请参阅第 2.4.6 节:

https://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

否则请参阅 Hibernate 过滤器:

Hibernate3 能够预先定义过滤条件并附加 这些过滤器在类级别和集合级别。一个过滤器 条件允许您定义一个限制子句,类似于 类和各种可用的现有“where”属性 集合元素。然而,这些过滤条件可以是 参数化。然后应用程序可以在运行时决定是否 应启用某些过滤器及其参数值 应该。过滤器可以像数据库视图一样使用,但它们是 在应用程序内部进行参数化。

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/filters.html

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多