一起坚持@OneToMany和@ManyToMany

时间:2018-11-15 06:48:22

标签: java spring-boot jpa orm

我正在处理一个项目。我使用springboot和Jpa。它可以完美运行并在Program运行时将数据插入DB。但是当我想通过邮递员保存数据时,它显示BadRequest。如何为此创建控制器类的。喜欢:

CrickerPlayer:

public class CricketPlayer extends PlayerProfile  {
    @Id
    private String playerId;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cricketPlayer")
    private List<Performance> performanceList;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "play_for_Team")
    private List<PlayForTeam> playForTeamList;
//getters and setters
    }

PlayerProfile:

public class PlayerProfile {
    private String image;
    private String name;
    private int age;
    private LocalDate dob;
    private double height;
    private String nationality;
    private int jerseyNo;
    private String game;
}

Performance.class

public class Performance {
    @Id
    private String performanceId;
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "id")
    private Game match;
    private long[] runsScored;
    private int fours;
    private int sixes;
    private int ballsFaced;
    private String[] oversBowled;
    private int bowled;
    private int wickets;
    private double BattingAverage;
    private double BowlingAverage;
    private double economy;
    private int totalStumping;
    private double strikeRate;
    private int highestScore;
    private int totalMaidens;
    private int totalCatches;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Role> performanceRoleList;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "player_id")
    private CricketPlayer cricketPlayer;
}

还有一些实体,例如地面,系列游戏和Gametype枚举。喜欢: Game.class

public class Game {
    @Id
    private String id;
    @Enumerated
    private MatchType matchType;
    @OneToOne(cascade = CascadeType.ALL)
    private Ground ground;
    @OneToOne(cascade = CascadeType.ALL)
    private Series series;
    @OneToMany(cascade = CascadeType.ALL)
    private List<CricketPlayer> cricketPlayers;
}

Ground.class

public class Ground {
   @Id
   private String groundId;
   private String groundName;
}

MatchType.enum

public enum MatchType {
    ODI,
    TEST,
    T20,
    T10;
}

RestController.class

@Controller
@RequestMapping(value = "api/cricketplayer")
@AllArgsConstructor
public class CricketPlayerRest {

    @Autowired
    private final CricketPlayerService cricketPlayerService;


    private  Game game1;
    private Ground ground1;
    private Series series1;
    private Performance performance1;
    private Role role1;
    private PlayForTeam playForTeam1;
    private CricketPlayer cricketPlayer1;

    private WrapperClass wrapperClass;


    List<Performance> performanceList = new ArrayList<>();
    List<PlayForTeam> playForTeamList = new ArrayList<>();
    List<Role> roleList = new ArrayList<>();
    List<CricketPlayer> playForTeamCricketer = new ArrayList<>();
    List<CricketPlayer> gameListForCricketer = new ArrayList<>();

    @GetMapping(value = "all")
    @ResponseBody
    public ResponseEntity<?> getAllPlayers() {
        return ResponseEntity.status(HttpStatus.OK).body(cricketPlayerService.findAllPlayers());
    }

    @GetMapping(value = "name")
    @ResponseBody
    public ResponseEntity<?> getPlayerByName(@RequestParam String name) {

        Optional<CricketPlayer> cricketPlayer = cricketPlayerService.getPlayerByName(name);

        if (cricketPlayer.isPresent()) {
            return ResponseEntity.status(HttpStatus.OK).body(cricketPlayer.get());
        } else {
            return ResponseEntity.badRequest().body(null);
        }

    }


    @GetMapping(value = "{id}")
    public ResponseEntity<?> getPlayerById(@PathVariable String id) {

        Optional<CricketPlayer> cricketPlayer = cricketPlayerService.findPlayersById(id);

        if (cricketPlayer.isPresent()) {
            return ResponseEntity.status(HttpStatus.OK).body(cricketPlayer.get());
        } else {
            return ResponseEntity.badRequest().body(null);
        }

    }

    @PostMapping(value = "save",
            produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<?> createPlayer(@RequestBody CricketPlayer cricketPlayer) {
        try {



           CricketPlayer createdPlayer = cricketPlayerService.save(cricketPlayer);
            ResponseEntity<CricketPlayer> cricketPlayerResponseEnity = ResponseEntity
                    .status(HttpStatus.CREATED)
                    .body(createdPlayer);
            return cricketPlayerResponseEnity;
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(null);
        }

    }

}

每个实体都有存储库。 谁能为这个项目提供帮助,我该如何通过设置每个实体来更新cricketPlayer。

我的项目链接是:You see it here also in github

Postman data:
{
        "image": "Musta",
        "name": "Musta",
        "age": 28,
        "dob": "1985-04-02",
        "height": 167,
        "nationality": "Bangladeshi",
        "jerseyNo": 28,
        "game": "Cricket",
        "playerId": "Musta",
        "performanceList": [
            {
                "performanceId": "BD-NZ",
                "match": {
                    "id": "1",
                    "matchType": "ODI",
                    "ground": {
                        "groundId": "Mirpur",
                        "groundName": "SHERE Bangla"
                    },
                    "series": {
                        "seriesId": "Walton",
                        "seriesName": "walton Series"
                    }
                },
                "runsScored": [
                    2,
                    0,
                    1,
                    3,
                    4,
                    6
                ],
                "fours": 4,
                "sixes": 2,
                "ballsFaced": 12,
                "oversBowled": [
                    "w",
                    "2",
                    "6",
                    "1",
                    "4",
                    "0"
                ],
                "bowled": 0,
                "wickets": 1,
                "economy": 5.4,
                "totalStumping": 1,
                "strikeRate": 154.4,
                "highestScore": 154,
                "totalMaidens": 0,
                "totalCatches": 5,
                "performanceRoleList": [
                    {
                        "roleId": 3,
                        "type": "All-Rounder"
                    }
                ],
                "bowlingAverage": 31.6,
                "battingAverage": 25
            }
        ],
        "playForTeamList": [
            {
                "pftId": "sun",
                "team": {
                    "id": "sun",
                    "teamName": "sun"
                },
                "startsDate": "2011-03-01",
                "endsDate": "2013-03-31"
            }
        ]
    }

0 个答案:

没有答案