Home > database >  SpringBoot POST Request with ManyToOne relation
SpringBoot POST Request with ManyToOne relation

Time:09-12

I am currently playing around with SpringBoot and wanna create a little API that allows me to save and fetch persistent data. I can't find the right solutions online, thats why I asking here. Creating an entity and with that a database table was very easy to do, and so was the implementation of the POST and GET request.

I have a very basic idea here. I have a table of players. Each of those players can participate in a foosball game, taking one of the four possible positions.

One player can have multiple games. A game can have one player (For each field).

Because of how easy everything was till the entity relation, I would assume that SpringBoot can automatically fetch the right player based on the id, that is inside of the POST request. But at the moment my application just throws an error, because my players are null and I made them non-nullable.

Do I need to manually fetch the player from the PlayerRepository and append them on the game object or do I miss some annotations? What would be the best practice to pull of those four API calls?

That how I would design my POST request:

{
    "attackBlackPlayerId": 1,
    "attackYellowPlayerId": 2,
    "defenseBlackPlayerId": 3,
    "defenseYellowPlayerId": 4,
    "black_won": true
}
@Entity
public class Player {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Column(nullable = false)
    private String firstName;

    @Column(nullable = false)
    private String lastName;

    private String email;

    @CreationTimestamp
    @Column(nullable = false)
    private LocalDateTime creationDate;
}

@Entity
public class Game {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @CreationTimestamp
    @Column(nullable = false)
    private LocalDateTime playDateTime;

    @ManyToOne(optional = false)
    @JoinColumn(nullable = false)
    private Player attackBlackPlayer;

    @ManyToOne(optional = false)
    @JoinColumn(nullable = false)
    private Player defenseBlackPlayer;

    @ManyToOne(optional = false)
    @JoinColumn(nullable = false)
    private Player attackYellowPlayer;

    @ManyToOne(optional = false)
    @JoinColumn(nullable = false)
    private Player defenseYellowPlayer;

    @Column(nullable = false)
    private boolean blackWon;
}
@RestController
public class API {

    @Autowired
    private PlayerRepository playerRepository;

    @Autowired
    private GameRepository gameRepository;

    @GetMapping("/players")
    public @ResponseBody Iterable<Player> getPlayers() {
        return playerRepository.findAll();
    }

    @PostMapping("/player")
    public @ResponseBody Player addPlayer(@RequestBody Player player) {
        return playerRepository.save(player);
    }

    @GetMapping("/games")
    public @ResponseBody Iterable<Game> getGames() {
        return gameRepository.findAll();
    }

    @PostMapping("/game")
    public @ResponseBody Game addGame(@RequestBody Game game) {
        return gameRepository.save(game);
    }
}

Your Lord Tkay

CodePudding user response:

The @OneToMany and @ManyToOne annotations have fields which must be correctly initialized if you want that the mapping works as expected.

Example :

   @Entity
   public class Employee {
 
       @Id
       private Long id;

       @OneToMany(fetch = FetchType.LAZY, mappedBy = "employee")
       private List<Email> emails;
   }

   @Entity
   public class Email {
 
       @ManyToOne(fetch = FetchType.LAZY)
       @JoinColumn(name = "employee_id")
       private Employee employee;
   }
  • Related