JPA更改不会保留在数据库中

时间:2016-07-27 10:05:23

标签: java spring jpa

我对春天很新,我正在努力建立一个休息api。

我的目标是创建一个小型的问答游戏。

游戏由Rounds(1:n)组成。

在Round中,我有用于设置用户响应的字段。

这是我的RoundRepository类:

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.utouch.nymeria.quizzServer.models.Round;

public interface RoundRepository extends JpaRepository<Round, Long> {

    @Modifying
    @Query("update Round r set r.reply_player = ?1 where r.id = ?2")
    @Transactional
    void addPlayerReply(int reply_number, Long roundID);

    @Modifying
    @Query("update Round r set r.reply_opponent = ?1 where r.id = ?2")
    @Transactional
    void addOpponentReply(int reply_number, Long roundID);
}

GameRepository.java:

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.utouch.nymeria.quizzServer.models.Game;
import org.utouch.nymeria.quizzServer.models.User;

public interface GameRepository extends JpaRepository<Game, Long> {
    List<Game> findByPlayer(User player);
}

Game.java:

@Entity
public class Game implements Serializable {

    private static final long serialVersionUID = 2L;

    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne
    private User player;

    @ManyToOne
    private User opponent;

    private int player_score=0;
    private int opponent_score=0;

    private Boolean ended;

    @Temporal(TemporalType.TIMESTAMP)
    private Date startDate = new Date();

    @OneToMany(targetEntity=Round.class,
               fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    private List<Round> rounds = new ArrayList<Round>();

    public Game(User player, User opponent) {
        super();
        this.player = player;
        this.opponent = opponent;
        this.ended = false;
    }

    public Game() {
        super();
        this.ended = false;
    }
    // ... getter / setter
}

在这里,我的逻辑:

// create user
User jesaispas = userRepo.save(new User("jesaispas","pass"));
User morgan = userRepo.findByLogin("morgan");

// create first game
Game game1 = new Game(morgan,jesaispas);
gameRepo.save(game1);

// add first round
Round r1 = new Round(q1.getIdQuestion());
//roundRepo.save(r1);

game1.getRounds().add(r1);
gameRepo.save(game1);

roundRepo.addPlayerReply(3, r1.getId());
roundRepo.addOpponentReply(1, r1.getId());


Round r2 = new Round(q2.getIdQuestion());
//roundRepo.save(r2);

game1.getRounds().add(r2);
gameRepo.save(game1);

roundRepo.addPlayerReply(4, r2.getId());

还有hibernate日志:

Hibernate: insert into round (round_id, questionid, reply_opponent, reply_player, start_date) values (null, ?, ?, ?, ?)

Hibernate: insert into game_rounds (game, rounds) values (?, ?)

Hibernate: update round set reply_player=? where round_id=?

Hibernate: update round set reply_opponent=? where round_id=?

Hibernate: select game0_.game_id as game_id1_0_1_, game0_.ended as ended2_0_1_, game0_.opponent as opponent6_0_1_, game0_.opponent_score as opponent3_0_1_, game0_.player as player7_0_1_, game0_.player_score as player_s4_0_1_, game0_.start_date as start_da5_0_1_, rounds1_.game as game1_0_3_, round2_.round_id as rounds2_1_3_, round2_.round_id as round_id1_3_0_, round2_.questionid as question2_3_0_, round2_.reply_opponent as reply_op3_3_0_, round2_.reply_player as reply_pl4_3_0_, round2_.start_date as start_da5_3_0_ from game game0_ left outer join game_rounds rounds1_ on game0_.game_id=rounds1_.game left outer join round round2_ on rounds1_.rounds=round2_.round_id where game0_.game_id=?

Hibernate: select user0_.id as id1_4_0_, user0_.auth_token as auth_tok2_4_0_, user0_.email as email3_4_0_, user0_.last_activity as last_act4_4_0_, user0_.login as login5_4_0_, user0_.password as password6_4_0_, user0_.registration_date as registra7_4_0_, user0_.salt as salt8_4_0_ from user user0_ where user0_.id=?

Hibernate: select user0_.id as id1_4_0_, user0_.auth_token as auth_tok2_4_0_, user0_.email as email3_4_0_, user0_.last_activity as last_act4_4_0_, user0_.login as login5_4_0_, user0_.password as password6_4_0_, user0_.registration_date as registra7_4_0_, user0_.salt as salt8_4_0_ from user user0_ where user0_.id=?

Hibernate: insert into round (round_id, questionid, reply_opponent, reply_player, start_date) values (null, ?, ?, ?, ?)

Hibernate: insert into round (round_id, questionid, reply_opponent, reply_player, start_date) values (null, ?, ?, ?, ?)

Hibernate: insert into game_rounds (game, rounds) values (?, ?)

Hibernate: insert into game_rounds (game, rounds) values (?, ?)

Hibernate: update round set reply_player=? where round_id=?

The problem is: the modification is not persisted, but as week can see, there is update request done by hibernate. So, Why get I do a ```findAll()``` the value is not updated ?

你知道问题出在哪里吗?

1 个答案:

答案 0 :(得分:2)

@Transactional置于已实现的JpaRepository类(实现RoundRepository的类)之上。然后,您应该能够看到您的数据永久保留。