'How to join 3 tables or fetch data from 3 tables and create one using in Spring JPA

I have 3 models/tables/entities in spring boot project.

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

I would like to have the 4th one by joining above listed tables to get information like(when work started on chair, who worked on chair, what's chair's current stage, when work on chair completed etc...) it should look like this

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

in product_lifecycle table i can see that 2 people worked on chair and both worked 2 hrs and chair is completed.

Relationship

  • An employee can work on different products
  • A single products can be worked by many employees
  • A product can have many stages
  • A single stage can be used for many products

How can i do this at class level? Need help



Solution 1:[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

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jrsall92