简体   繁体   English

具有多对一关系的 SpringBoot POST 请求

[英]SpringBoot POST Request with ManyToOne relation

I am currently playing around with SpringBoot and wanna create a little API that allows me to save and fetch persistent data.我目前正在玩 SpringBoot 并想创建一个小 API 允许我保存和获取持久数据。 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.创建一个实体和一个数据库表非常容易,POST 和 GET 请求的实现也是如此。

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.由于在实体关系之前一切都很容易,我假设 SpringBoot 可以根据 POST 请求中的 id 自动获取正确的播放器。 But at the moment my application just throws an error, because my players are null and I made them non-nullable.但目前我的应用程序只是抛出一个错误,因为我的播放器是 null 并且我使它们不可为空。

Do I need to manually fetch the player from the PlayerRepository and append them on the game object or do I miss some annotations?我是否需要在游戏 object 上手动从 PlayerRepository 和 append 获取播放器,还是我错过了一些注释? What would be the best practice to pull of those four API calls?提取这四个 API 调用的最佳实践是什么?

That how I would design my POST request:那我将如何设计我的 POST 请求:

{
    "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你的主

The @OneToMany and @ManyToOne annotations have fields which must be correctly initialized if you want that the mapping works as expected.如果您希望映射按预期工作,@OneToMany 和 @ManyToOne 注释具有必须正确初始化的字段。

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;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM