JpaRespository deleteBy ...在某些情况下不起作用

时间:2019-02-18 12:14:48

标签: java mysql spring hibernate spring-data-jpa

我正在尝试制作一个简单的基于时间的策略游戏... 这是数据库连接:Kingdom(OneToMany)->进度,建筑物,资源,部队 mySQL connection kingdom(OneToMany) -> progression, building, resource, troop

如果我这样做:

public void updateProgression(Kingdom kingdom) throws TroopNotFoundException, BuildingNotFoundException {
        List<ProgressionModel> progressions = kingdom.getKingdomsProgresses();
        for (ProgressionModel p : progressions) {
            System.out.println(p.getId());
            System.out.println(p.getType());
            System.out.println(p.getGameObjectId());
            System.out.println(p.getProgressKingdom().getId());
            if (timeService.timeIsUp(p)) {
                progress(p, kingdom);
            }
        }
   }
    private void progress(ProgressionModel progressionModel, Kingdom kingdom) throws TroopNotFoundException,
            BuildingNotFoundException {
        if (progressionModel.getGameObjectId() == null) {
            if (progressionModel.getType().equals("TROOP")) {
                troopService.createTroop(kingdom);
                progressionModelRepository.deleteById(progressionModel.getId());
                return;
            }
            buildingService.createBuilding(progressionModel, kingdom);
            System.out.println(progressionModel.getId());
            progressionModelRepository.deleteById(progressionModel.getId());
            return;

控制台中的休眠查询:

Hibernate: select kingdomspr0_.kingdom_id as kingdom_5_3_0_, kingdomspr0_.id as id1_3_0_, kingdomspr0_.id as id1_3_1_, kingdomspr0_.game_object_id as game_obj2_3_1_, kingdomspr0_.kingdom_id as kingdom_5_3_1_, kingdomspr0_.time_to_progress as time_to_3_3_1_, kingdomspr0_.type as type4_3_1_ from progression kingdomspr0_ where kingdomspr0_.kingdom_id=?
1
FARM
null
1
Hibernate: select kingdomsre0_.kingdom_id as kingdom_7_4_0_, kingdomsre0_.id as id2_4_0_, kingdomsre0_.id as id2_4_1_, kingdomsre0_.amount as amount3_4_1_, kingdomsre0_.resource_per_minute as resource4_4_1_, kingdomsre0_.kingdom_id as kingdom_7_4_1_, kingdomsre0_.type as type5_4_1_, kingdomsre0_.updated_at as updated_6_4_1_, kingdomsre0_.dtype as dtype1_4_1_ from resources kingdomsre0_ where kingdomsre0_.kingdom_id=?
Hibernate: update resources set amount=?, resource_per_minute=?, kingdom_id=?, type=?, updated_at=? where id=?
Hibernate: insert into buildings (hp, kingdom_id, level, type) values (?, ?, ?, 'Farm')
1

它不会删除进度...

但是,如果我调用内置查询的JpaRepository以在updateProgression方法中获取属于王国的所有进程,则:

List<ProgressionModel> progressions = progressionModelRepository.findAllByProgressKingdom(kingdom);

然后休眠从进度表中删除进度(我要在第一种情况下实现):

Hibernate: select progressio0_.id as id1_3_, progressio0_.game_object_id as game_obj2_3_, progressio0_.kingdom_id as kingdom_5_3_, progressio0_.time_to_progress as time_to_3_3_, progressio0_.type as type4_3_ from progression progressio0_ where progressio0_.kingdom_id=?
1
FARM
null
1
Hibernate: select kingdomsre0_.kingdom_id as kingdom_7_4_0_, kingdomsre0_.id as id2_4_0_, kingdomsre0_.id as id2_4_1_, kingdomsre0_.amount as amount3_4_1_, kingdomsre0_.resource_per_minute as resource4_4_1_, kingdomsre0_.kingdom_id as kingdom_7_4_1_, kingdomsre0_.type as type5_4_1_, kingdomsre0_.updated_at as updated_6_4_1_, kingdomsre0_.dtype as dtype1_4_1_ from resources kingdomsre0_ where kingdomsre0_.kingdom_id=?
Hibernate: update resources set amount=?, resource_per_minute=?, kingdom_id=?, type=?, updated_at=? where id=?
Hibernate: insert into buildings (hp, kingdom_id, level, type) values (?, ?, ?, 'Farm')
1
Hibernate: delete from progression where id=?

我无法解决这个问题。当然,如果我使用存储库调用来获取属于王国的进度,则代码可以工作,但是我真的不理解为什么即使我向其传递了有效的ID,deleteById也不执行任何操作。谢谢您的帮助!我希望构图足够好理解。这是我关于StackOverflow的第一个问题,所以请向我提出建设性的批评。

1 个答案:

答案 0 :(得分:1)

您正在为JPA的写后行为而烦恼。

当您删除实体或以任何方式更新它时,JPA不会立即将更改写入数据库。 实际写入仅在刷新事件发生时发生,或者在出于其他原因(例如为新创建的实体获取ID)所必需的情况下发生。

在以下情况下会触发刷新事件:

  1. 交易结束。
  2. 如果查询数据库。 这是为了确保您在查询中看到更新的数据。 如果需要,还可以将其配置为不触发刷新。
  3. 如果您显式调用EntityManager上的flush。如果您使用的是Spring Data JPA,则可以使用JpaRepository and calling flush() on it来完成。