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

标签 java spring jpa

我对 Spring 还很陌生,我正在尝试构建一个 Rest api。

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

游戏由回合(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);
}

游戏.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 ?

你知道问题出在哪里吗?

最佳答案

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

关于java - JPA 更改不会保留在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38609855/

相关文章:

java - 无法解析导入 javax.servlet.ServletRegistration

spring - 基于 Maven 的项目使用 Spring Hibernate、EJB3.0、JPA JBOSS?

java - JPA从多进程写入单个数据库

java - 这个 JPQL 查询字符串有什么问题

java - JPA中的乐观锁是什么?

java - 导入有效的包会导致 java 编译器错误

java - 如何在运行时将 JSR-045 SMAP 信息添加到 Java 堆栈跟踪?

java - 覆盖父类(super class)构造函数

java - Jsoup 无法正常工作

css - Springboot - 资源解释为样式表但以 MIME 类型文本/htm 传输