0
我对Spring很陌生,我试图构建一个休息API。JPA更改不会持久存在数据库中
我的目的是创建一个小问答游戏。
游戏由Rounds(1:n)组成。
在回合中,我有设置用户响应的字段。
这是我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 ?
你知道所在的问题呢?
我们可以看到你的'addPlayerReply(..)'的实现请 – jr593
没有实现,通常一切都应该通过注释完成,对吧?其实,这项工作,但只有一次,刷新后,数据不会被记住 – Nymeria
你还可以发布你的'游戏','圆'和'GameRepo'代码吗?你能在持续期间看到任何错误吗? –