0

I have a problem. I don't know how to create an API when I have other entities. I work with Postman and when I do a request to see all projects from the database I want to receive the entities also.

For example this is my entity:

@Entity
@Table(name = "project")
public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "proj_id")
    private int projectId;

    @Column(name = "project_name")
    private String projectName;

    @Column(name = "dg_number")
    private int dgNumber;

    @ManyToMany
    @JoinTable(name = "project_gate_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "gate_id"))
    @JsonBackReference
    private  List<Gate> gates;

    @ManyToMany
    @JoinTable(name = "project_threshold_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "thresholdgates_id"))
    @JsonBackReference
    private  List<Threshold> thresholds;

This is Gate entity

@Entity
@Table(name = "gate")
public class Gate {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "gate_id")
    private int gateId;

    @Column(name = "gate_type")
    private String gateType;

    @Column(name = "gate_value")
    private float value;

Threshold entity

@Entity
@Table(name = "threshold")
public class Threshold {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "threshold_id")
    private int thresholdId;

    @Column(name = "threshold_value")
    private int thresholdValue;

    @Column(name = "threshold_type")
    private String thresholdType;

Controller

@RestController
@RequestMapping(ProjectController.PROJECT_URL)
public class ProjectController {

    public static final String PROJECT_URL = "/cidashboard/projects";

    @Autowired
    private final ProjectService projectService;

    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }

    @GetMapping
    public List<Project> getAllProjects(){
        return projectService.findAllProjects();
    }

    @GetMapping("/{id}")
    public Project getProjectById(@PathVariable int id) {
        return projectService.findProjectById(id);
    }

    @PostMapping
 //   @Consumes(MediaType.APPLICATION_JSON_VALUE)
    public Project saveProject(@RequestBody Project newProj) {
        return projectService.saveProject(newProj);
    }
}

When I do a Get request in Postman, I receive this output for example:

 {
        "projectId": 1,
        "projectName": "jenkins",
        "dgnumber": 1
    }, 

I expect to receive information about gate and threshold also. I don't understand how to do this things more exact.

1

2 Answers 2

1

related entities are not loaded by default in JPA. you have to define fetch = FetchType.EAGER in @ManyToMany relationship

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "project_gate_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "gate_id"))
@JsonBackReference
private  List<Gate> gates;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "project_threshold_relation", joinColumns = @JoinColumn(name = "proj_id"), inverseJoinColumns = @JoinColumn(name = "thresholdgates_id"))
@JsonBackReference
private  List<Threshold> thresholds;
Sign up to request clarification or add additional context in comments.

Comments

0

Data associated with a @ManyToMany is by default lazily loaded. You need to specify that you want in eagerly loaded (if you are using spring-data, entity graphs can be used for that).

Please see the link for details: Spring Data JPA And NamedEntityGraphs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.