gpt4 book ai didi

java - 实体未在实体监听器@PostPersist(Spring 数据 jpa)上更新

转载 作者:搜寻专家 更新时间:2023-10-30 22:27:34 25 4
gpt4 key购买 nike

我有下一个 ER 图 enter image description here
我想在每次保存 Score 实体时更新 PlayerStat 实体。所以我尝试使用 @PostPersist 注释。当我保存 Score 实体时,调用该注释的函数,但我在此函数中所做的更改不会持续存在。我的实现如下。所有实体类都有构造函数的 lombok 注释。
播放器

@Id
@GeneratedValue(generator = "custom-uuid")
@GenericGenerator(name = "custom-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

@Temporal(TemporalType.DATE)
@Getter
@NonNull
private Date createdAt;

@Column(unique = true)
@Getter
@Setter
@NonNull
private String nickname;

@OneToMany(mappedBy = "winner")
@Getter
@NonNull
private Set<Game> gamesWon;

@Getter
@OneToOne(cascade = CascadeType.PERSIST, optional = false)
@JoinColumn
private PlayerStat stat;

@OneToMany(mappedBy = "player")
@Getter
@NonNull
private Set<Score> scores;

public Player(String nickname, Date createdAt) {
this.nickname = nickname;
this.createdAt = createdAt;
stat = new PlayerStat(0, 0, 0, this);
}

游戏

@Id
@GeneratedValue(generator = "custom-uuid")
@GenericGenerator(name = "custom-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

@Getter
@NonNull
@Column(updatable = false)
private Integer durationMinutes;

@Temporal(TemporalType.DATE)
@Getter
@NonNull
private Date dateStarted;

@ManyToOne(optional = false)
@JoinColumn
@Getter
@NonNull
private Player winner;

@OneToMany(mappedBy = "game")
@Getter
private Set<Score> scores;

@PostPersist
private void update(){
System.out.println("On game post persist");
winner.getStat().incrementTotalWins();
}

玩家统计

@Id
@GeneratedValue(generator = "custom-uuid")
@GenericGenerator(name = "custom-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

@Getter
@Setter
@NonNull
private Integer totalScore;

@Getter
@NonNull
private Integer totalWins;

@Getter
@NonNull
private Integer totalGames;

@Getter
@OneToOne(mappedBy = "stat", optional = false)
@NonNull
private Player player;

public void incrementTotalWins() {
totalWins++;
}

public void incrementTotalGames() {
totalGames++;
}

得分

@Id
@GeneratedValue(generator = "custom-uuid")
@GenericGenerator(name = "custom-uuid", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;


@ManyToOne(optional = false)
@JoinColumn(name = "game_id")
@Getter
@NonNull
private Game game;

@ManyToOne(optional = false)
@JoinColumn
@Getter
@NonNull
private Player player;

@Getter
@NonNull
private Integer score;



@PostPersist
void updatePlayerStat(){
System.out.println("On score post persist");
player.getStat().incrementTotalGames();
player.getStat().setTotalScore(player.getStat().getTotalScore() + score);
System.out.println(player);
}

因此,我尝试保存单个Player、与该玩家的Game,以及与该游戏和玩家的Score。这两个函数都调用了 @PostPersist 注释。然后在另一个请求中我读了这个播放器。应用了游戏功能中的更改,但未应用得分功能中的更改。但是如果我像这样单独请求(它与实体监听器无关)

score.getPlayer().getStat().incrementTotalGames();
score.getPlayer().getStat().incrementTotalWins();
score.getPlayer().getStat().setTotalScore(100);
scoreRepository.save(score);

将应用所有更改。
我还尝试使用单独的 EntityListener 类并进入该类存储库,保存修改后的对象,但这也不起作用。我尝试在每个关系中添加 CascadeType.ALL ,仍然没有变化。那么我应该如何自动更新 PlayerStat?

最佳答案

如果您使用@PostPersist,信息将在持久化后更新,因此您不会在数据库中拥有信息,因此您应该在将结果保存到数据库之前在服务层执行此增量操作,你也可以试试:

1- @PrePersist 因为 PrePersist 在实体管理器持久化操作实际执行或级联之前执行。此调用与持久化操作同步。

2-@PreUpdate 因为 PreUpdate 在数据库UPDATE 操作之前 执行。

关于java - 实体未在实体监听器@PostPersist(Spring 数据 jpa)上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49278593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com