0

I am trying to change the task status (there are 3 permanent tasks in the database: To do, In progress, Done). I tried to use Spring Data JPA's built-in REST and the patch method but it doesn't work

enter image description here

The next step was to do the logic manually:

TaskServiceImpl

@Service
@AllArgsConstructor
public class TaskServiceImpl {

    private TaskRepository taskRepository;
    private StatusRepository statusRepository;

    @Transactional
    public Task updateTaskStatus(int taskId,int statusId){
        Task task=taskRepository.findById(taskId).orElseThrow(() -> new ResourceNotFoundException("Task not found"));
        Status status=statusRepository.findById(statusId).orElseThrow(() -> new ResourceNotFoundException("Status not found"));
        task.setStatus(status);
        taskRepository.save(task);
        return task;
    }

}

TaskController

@RestController
@RequestMapping("/api/tasks")
@AllArgsConstructor
@CrossOrigin(origins = "http://localhost:4200")
public class TaskController {

    private final TaskServiceImpl taskService;

    @PatchMapping("/update/{id}")
    public ResponseEntity<Task> updateTaskStatus(@PathVariable("id") int id, @RequestBody Map<String, Integer> status) {
        taskService.updateTaskStatus(id, status.get("status"));
        return ResponseEntity.ok().build();
    }

}

but still the task has not changed status.

Task entity

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table
@Getter
@Setter
public class Task {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne
    @JoinColumn(name = "project_id", nullable = false)
    private Project project;


    @ManyToOne
    @JoinColumn(name = "status_id", nullable = false)
    private Status status;

}

Status entity

package com.example.todo.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.util.HashSet;
import java.util.Set;

@Entity
@Table
@Getter
@Setter
public class Status {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;

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

    @OneToMany(mappedBy = "status",cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    private Set<Task> tasks=new HashSet<>();

    public void addTask(Task task){
        if(task!=null){
            if(tasks==null) tasks=new HashSet<>();
            tasks.add(task);
            task.setStatus(this);
        }
    }
}

I looked for a solution in similar posts but nothing helped me

  • @K.Nicholas Task entity. The ManyToOne annotation with the JoinColumn annotation is used on the Task entity to create the relationship to the Status entity. This means that the foreign key to the Status table will be stored in the Task table. – stealthoust Feb 20 '23 at 18:16
  • I change the logic of method `@Transactional public Task updateTaskStatus(int taskId,int statusId){ Task task=taskRepository.findById(taskId).orElseThrow(() -> new ResourceNotFoundException("Task not found")); Status status=statusRepository.findById(statusId).orElseThrow(() -> new ResourceNotFoundException("Status not found")); task.setStatus(status); status.getTasks().add(task); statusRepository.save(status); return task; }` And that works – stealthoust Feb 20 '23 at 18:41
  • That would be the "You can persist it yourself" solution. – K.Nicholas Feb 20 '23 at 18:42
  • So how can i improve this code? – stealthoust Feb 20 '23 at 18:46
  • Is `@ManyToOne` FetchType.Eager or Lazy? If it's eager, why are you fetching it yourself? – K.Nicholas Feb 20 '23 at 19:02

0 Answers0