I have a Springboot Java project and I have a Workflow Model and a Form Model, the…
- User
I have a Springboot Java project and I have a Workflow Model and a Form Model, the relationship they share is a Workflow can own many forms and a form can be owned by many workflow. Additionally, the Workflow model must contain the ordering of the form, for instance, if a Workflow owns 3 forms, A, B, C, he must store the ordering of the forms so that we will know which forms comes first. Below is what I have so far.
package com.vms.model;
import jakarta.persistence.*; import lombok.*;
import java.util.ArrayList; import java.util.List;
@Data @Builder @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "form") public class Form {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
@Column(nullable = false, unique = true) private String name;
@Column(nullable = false) private String description;
@Column(name = "is_finished", nullable = false) private boolean isFinished;
// If an instance of form is deleted, all associated form sections will be deleted @OneToMany(mappedBy = "form", cascade = CascadeType.ALL, orphanRemoval = true) private List formSections; @ManyToMany @JoinTable(name = "form_account", joinColumns = @JoinColumn(name = "form_id"), inverseJoinColumns = @JoinColumn(name = "account_id")) private List authorizedAccounts; } package com.vms.model;
import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor;
@Data @Builder @Entity @NoArgsConstructor @AllArgsConstructor @Table(name = "workflow") public class Workflow { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
@Column(nullable = false, unique = true) private String name;
@Column(nullable = false) private Boolean progress;
@Column(nullable = false) private Boolean isDraft; }
- Assistant
To implement the relationship between Workflow and Form, you can add a new entity, let's call it , to represent the many-to-many relationship between them. This entity can also have an additional attribute to store the order of the forms within a workflow. Here's an example implementation:
- User
In this example,
@ManyToOne @JoinColumn(name = "workflow_id") private Workflow workflow;
@ManyToOne @JoinColumn(name = "form_id") private Form form;
Do you not have to specify @OneToMany on the other end of the relationship (e.g. the MappedBy)
- Assistant
You're correct! I apologize for the mistake. To complete the bidirectional relationship between
WorkflowandForm, you should also add themappedByattribute to the@OneToManyannotation in theFormclass. Here's an updated implementation:kotlinpackage com.vms.model; import jakarta.persistence.*; import lombok.*; import java.util.ArrayList; import java.util.List; @Data @Builder @NoArgsConstructor @AllArgsConstructor @Entity @Table(name = "form") public class Form { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false, unique = true) private String name; @Column(nullable = false) private String description; @Column(name = "is_finished", nullable = false) private boolean isFinished; // If an instance of form is deleted, all associated form sections will be deleted @OneToMany(mappedBy = "form", cascade = CascadeType.ALL, orphanRemoval = true) private List<FormSection> formSections; @ManyToMany(mappedBy = "forms") private List<Workflow> workflows; }