Skip to content

Instantly share code, notes, and snippets.

@ashutosh049
Last active October 12, 2021 19:17
Show Gist options
  • Save ashutosh049/6feb6758e04e7f088404fe7f4c74c94e to your computer and use it in GitHub Desktop.
Save ashutosh049/6feb6758e04e7f088404fe7f4c74c94e to your computer and use it in GitHub Desktop.
Problems making a many-to-many relationship work across 2 microservices using Spring Boot & Hibernate
Use case: assign an already existing task to an already existing user.
Yuo can assign multiple users at a time to 1 single task
post: /tasks/allocation/new
```json
{
"task-id": 12345,
"users": [
{
"username": "user-1",
"user-id": "101"
},
{
"username": "user-2",
"user-id": "102"
},
{
"username": "user-3",
"user-id": "103"
}
]
}
```
Post request is having one task and list of user-details to be allocated to that task.
@Data
public class TaskAllocation{
@jsonProperty("task-id")
private long taskId;
@JsonProperty("users")
private List<Users> userList;
}
public CompletableFuture<ResponseEntity<?>> assignTaskToUsers(@ResponseBody @Valid TaskAllocation taskAllocation){
// service call
}
# -----------------------------------
Inside service:
1. fetch the task from task db
2. If needed, fetch detaisl of users from user-service(for each user)
3. For each user:
1.Create new Allocation
2. Set task Id
3. Set user-id or username
4. Save
Note: Task is already existing
# Table Task-Allocation
----------------------------------------------------------------------
alloc-id(PK) task-Id user-id timetamp
----------------------------------------------------------------------
1 12345 101 123123123123
2 12345 102 123123123123
3 12345 103 123123123123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment