Home > Blockchain >  Not able to join 2 tables in spring data jpa
Not able to join 2 tables in spring data jpa

Time:01-25

I am new to spring-data-jpa. I am working on a task management system. I have 2 entities:

public class Task {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long taskId;
private String title;
private String description;
private Status status;

@OneToOne
@JoinColumn(name = "user_id", referencedColumnName = "userId")
private User assignee;

and:

@Entity
@Table(name = "tbl_user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long userId;
private String name;
private String email;
private Active active;
private String password;

}

I have an endpoint that creates a new task:

    @PostMapping
@ResponseStatus(HttpStatus.CREATED)
public TaskResponse addTask(@Valid @RequestBody Task task){
    return taskService.addTask(task);
}

This is the implementation:

    @Override
public TaskResponse addTask(Task task) {
    taskRepository.save(task);
    return mapToTaskResponse(task);
}

The error I get when I send a request is:

2023-01-24 15:10:01.825  WARN 1961 --- [nio-8080-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.tasksmanagement.entity.User` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (1); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.tasksmanagement.entity.User` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (1)<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 5, column: 17] (through reference chain: com.tasksmanagement.entity.Task["assignee"])]

I am not sure what am I doing wrong. Basically I create a user and send the id of this user in the request (the assignee field in the payload - screenshot attached) in order to assign the new task to that user. Can anyone please assist and help me understand what is the issue with the request? Should I send something else instead of the userId?

Thank you screenshot

CodePudding user response:

assignee is of type User not Integer, and yet you are sending assignee:1 which fails the deserialization of your request.

it should be rather

assignee:{ userId:1 }

but it will fail anyway on later on during atempt to persist the Task (but that is a different issue)

CodePudding user response:

So basically you want to create a Task for a User who has an id of 1.

All you need to do is map you User object with your Task object.

Try the following

@Override
public TaskResponse addTask(Task task) {
    User assignee = new User();
    assignee.setUserId(task.assignee);

    task.setAssignee(assignee);

    taskRepository.save(task);
    return mapToTaskResponse(task);
}
  • Related