0

I have very strange behavior. I have created repository which extends CrudRepository. All default methods works fine except save method. It doesnt save new entity to db. I need to save new entity with id which I provide. If I provide existing id in db - then save updates this entity successfully, but if I set new id - then nothing, not even error. My repo:

import com.xxx.yyy.entities.Game;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GameRepository extends CrudRepository<Game, Integer> {}

My entity:

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

import java.util.Objects;

@Table("game_info")
public class Game {

    @Id
    private int id;
    private String name;
    private int wager;

    setters and getters

My service:

public void addGame() {

    Game g = new Game();
    g.setId(10);
    g.setName("Name");
    g.saveWager(22);

    gameRepository.save(game);
}

My table:

CREATE TABLE `game_info`
(
    `id`        INT UNSIGNED NOT NULL COMMENT 'Unique identifier of the Game',
    `name`      VARCHAR(32)  NOT NULL COMMENT 'Name of the Game',
    `wager`     INT          NOT NULL COMMENT 'Wager of the Game',
    PRIMARY KEY (`id`),
    UNIQUE (`name`)
);
Anton Kolosok
  • 482
  • 9
  • 24

1 Answers1

1

It seams that its no way to save entity with seted id. I have to write new insert method like this:

@Modifying
@Query("INSERT INTO `game_info` (`id`, `name`, `wager`) VALUES (:id, :name, :wager)")
void addGame(@Param("id") int id, @Param("name") String name, @Param("wager") Integer wager);
Anton Kolosok
  • 482
  • 9
  • 24