java - 实体 JPA/Hibernate 中的条件 JoinColumn

标签 java spring hibernate jpa

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

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

相关文章:

java - 将随机端口暴露给 docker-compose.yml

java - 如何在 Spring MVC 列表中显示输入字段

java - 重新启动 Spring Boot 应用程序

java - Hibernate Named Queries 是真正意义上的预编译吗?

java - 使用内部类的 Jackson TypeReference

java - 在 Java Swing 中淡化部分图形

java - POM 文件失败,http ://cwiki. apache.org/confluence/display/MAVEN/UnresolvableModelException

java - 等待池完成线程

java - Hibernate 持久对象行为

hibernate - JBoss 在开发环境中找不到 PostgreSQL 的 JDBC 驱动程序