Can I use a void function ID Setter to avoid duplicateKeyException error?

54 Views Asked by At

Is it correct, if I create a Public void function where I add an object to a list with the same UUID, to avoid getting "duplicateKeyException: A different object with the same identifier value was already associated with the session" Error?

@Override
    public GameSuccessfullyDTO createGame(GameRequestDTO gameRequestDTO) throws ApiException {
        // Converts GameRequestDTO a Game
        Game game = gameRequestMapper.gameRequestDTOtoGame(gameRequestDTO);

        Optional<Team> optionalHomeTeam = teamRepository.findById(gameRequestDTO.getHomeTeam().getId());
        Optional<Team> optionalAwayTeam = teamRepository.findById(gameRequestDTO.getAwayTeam().getId());

        //Asigno el equipo local
        Team homeTeam = optionalHomeTeam.orElseThrow(() -> new TeamNotFoundException(gameRequestDTO.getHomeTeam().getId()));
        Team awayTeam = optionalAwayTeam.orElseThrow(() -> new TeamNotFoundException(gameRequestDTO.getAwayTeam().getId()));

        game.setStadium(homeTeam.getStadium());
        game.setHomeTeam(homeTeam);
        game.setAwayTeam(awayTeam);
        game.setUuid(UUID.randomUUID());
        BigDecimal spectatorsBigDecimal = BigDecimal.valueOf(gameRequestDTO.getSpectators());
        // Calcular revenue multiplicando spectatorsBigDecimal por 2
        game.setRevenue(spectatorsBigDecimal.multiply(BigDecimal.valueOf(3.5)));
        game.setTime(gameRequestDTO.getTime());
        game.setDate(gameRequestDTO.getDate());
        game.setHomeGoals(gameRequestDTO.getHomeGoals());
        game.setAwayGoals(gameRequestDTO.getAwayGoals());

        //IF I ADD THE GAME TO THE LIST HERE, IT THROWS THE EXCEPTION
        //homeTeam.getHomeGames().add(game);
        //awayTeam.getAwayGames().add(game);
    
        Game gameResponse = gameRepository.save(game);

        //I call thefunction here and it works.
        setUUIDx(homeTeam, awayTeam, gameResponse);
        return gameSuccessfullyDTOMapper.gameToGameSuccessfullyDTO(gameResponse);

    }    

public void setUUIDx(Team team, Team team2, Game game){
        team.getHomeGames().add(game);
        team2.getAwayGames().add(game);
    }
}

**TEAM CLASS**
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "team")
public class Team {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @OneToMany(mappedBy = "homeTeam", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @Column(nullable = true)
   // @JsonIgnore
    private List<Game> homeGames = new ArrayList<>();

    //Tener en cuenta JsonIgnoreProperties({"homeTeam", "awayTeam"})

    @OneToMany(mappedBy = "awayTeam", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @Column(nullable = true)
    //@JsonIgnore
    private List<Game> awayGames = new ArrayList<>();

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "division_id")
    private Division division;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "city_id")
    private City city;

    @OneToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "stadium_id")
    private Stadium stadium;

**GAME CLASS**
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "game")
public class Game {

    @Id
    private UUID uuid;
    private LocalDate date;
    private LocalTime time;
    @ManyToOne
    @JoinColumn(nullable = false, updatable = false)
    @JsonIgnore
    
    private Team homeTeam;
    @ManyToOne
    @JoinColumn(nullable = false, updatable = false)
    @JsonIgnore
    
    private Team awayTeam;
    private int homeGoals;
    private int awayGoals;
    @ManyToOne
    @JoinColumn(name = "stadium_id")
    private Stadium stadium;
    private int spectators;
    private BigDecimal revenue;
}
}

Another thing I'm not sure about is if I mapped the entities right.

0

There are 0 best solutions below