简体   繁体   English

实体JPA /休眠中的条件JoinColumn

[英]Conditional JoinColumn in Entity JPA/Hibernate

I do have a situation where I need to fetch data from three different tables based on some condition. 我确实需要根据某种条件从三个不同的表中获取数据。 Eg. 例如。

@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
}

I should able to insert and update the child tables using the Order entity. 我应该能够使用Order实体插入和更新子表。 How do I can this using JPA/Hibernate.. I searched a lot and i couldn't get any solution, apart from manually doing it at the service level instead of handling at the Entity Model 我如何使用JPA / Hibernate进行此操作。我进行了很多搜索,但没有找到任何解决方案,除了在服务级别手动进行操作(而不是在实体模型上进行处理)

You can use one of 2 non-JPA compliant, Hibernate specific features. 您可以使用两种非JPA兼容的特定于Hibernate的功能之一。 If the condition is static then see the Hibernate @Where annotation. 如果条件是静态的,请参见Hibernate @Where注释。 See section 2.4.6: 请参阅第2.4.6节:

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

Otherwise see Hibernate filters: 否则请参见休眠过滤器:

Hibernate3 has the ability to pre-define filter criteria and attach those filters at both a class level and a collection level. Hibernate3能够预定义过滤条件,并在类级别和集合级别附加这些过滤器。 A filter criteria allows you to define a restriction clause similar to the existing "where" attribute available on the class and various collection elements. 过滤条件允许您定义一个限制子句,该子句类似于类和各种集合元素上可用的现有“ where”属性。 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 https://docs.jboss.org/hibernate/orm/3.3/reference/zh/html/filters.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM