【问题标题】:How to join 3 tables or fetch data from 3 tables and create one using in Spring JPA如何在 Spring JPA 中连接 3 个表或从 3 个表中获取数据并创建一个表
【发布时间】:2022-01-22 14:15:53
【问题描述】:

我在 Spring Boot 项目中有 3 个模型/表/实体。

Employees
-----------
id    name

1     Tom
2     Bob


Products
-----------
id    name

1     Chair
2     Couch


Stages
----------------
id    name

1     Not Started
2     Started
3     In progress
4     Completed

我想通过加入上面列出的表格来获得第 4 个信息,例如(什么时候开始在椅子上工作,谁在椅子上工作,椅子的当前阶段是什么,什么时候完成椅子上的工作等等......)它应该看起来像这样

product_lifecycle
-----------------
id    product_id    stage_id    employee_id    started_date_time    end_date_time

1     1             3           1              01/22/2022 07:00AM   01/22/2022 09:00AM
2     1             4           2              01/22/2022 09:00AM   01/22/2022 11:00AM

product_lifecycle 表中,我可以看到 2 个人在椅子上工作,两人都工作了 2 小时,椅子完成了。

关系

  • 员工可以从事不同的产品工作
  • 一个产品可以多人合作
  • 一个产品可以有多个阶段
  • 一个平台可用于多种产品

我怎样才能在课堂上做到这一点? 需要帮助

【问题讨论】:

    标签: spring spring-boot hibernate jpa spring-data-jpa


    【解决方案1】:
    @Entity
    public class ProductLifeCycle {
    
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
    
      @ManyToOne
      private Product product;
    
      @ManyToOne
      private Stage stage;
    
      @ManyToOne
      private Employee employee;
    
      // getters and setters
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2021-04-01
      • 2014-01-22
      • 1970-01-01
      • 2023-04-03
      • 2015-05-22
      • 1970-01-01
      相关资源
      最近更新 更多