JPA事件监听器

huangapple 未分类评论55阅读模式
英文:

JPA event listener

问题

我需要构建一个从数据库获取记录的应用程序,这些记录等于某个值,如果返回 true,它将执行某些操作。我想知道 JPA 是否可以做到这一点。

我有一个实体 Game

  1. @Data
  2. @Entity
  3. public class Game {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private Long id;
  7. public Game() {
  8. }
  9. @OneToOne
  10. private Team teamBlue;
  11. @OneToOne
  12. private Team teamRed;
  13. @DateTimeFormat(pattern = "HH:mm:ss yyyy-MM-dd")
  14. private LocalDateTime localDateTime;
  15. }

以及返回 GameGameRepository

  1. @Repository
  2. public interface GameRepository extends JpaRepository<Game, Long> {
  3. @Override
  4. Optional<Game> findById(Long aLong);
  5. }

我想要检查 Game 中的 LocalDateTime 值是否等于当前的 LocalDateTimeLocalDateTime.now()),如果相等,则执行某些操作。
当然,我可以使用 @Scheduled 每隔 1 小时调用方法,但我想知道我所描述的是否可行。

简要总结:

数据库中的 Game 实体,其 LocalDateTime 值为 2020-04-05 21:00:00

当前日期时间:2020-04-05 12:00:00(不执行任何操作,因为当前日期时间不等于 Game 的日期时间)

[9 小时后]

数据库中的 Game 实体(不可变)其 LocalDateTime 值为 2020-04-05 21:00:00

当前日期时间:2020-04-05 21:00:00(当前日期时间与 Game 的日期时间匹配,因此执行某些操作(例如 System.out.println(game))。

英文:

I need to build an application that gets records from database, equales to some value and if it returns true it will do someee action. I was wondering if JPA can do something like that.

I have an entity Game:

  1. @Data
  2. @Entity
  3. public class Game {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private Long id;
  7. public Game() {
  8. }
  9. @OneToOne
  10. private Team teamBlue;
  11. @OneToOne
  12. private Team teamRed;
  13. @DateTimeFormat(pattern = &quot;HH:mm:ss yyyy-MM-dd&quot;)
  14. private LocalDateTime localDateTime;
  15. }

and GameRepository which returns the Game:

  1. @Repository
  2. public interface GameRepository extends JpaRepository&lt;Game, Long&gt; {
  3. @Override
  4. Optional&lt;Game&gt; findById(Long aLong);
  5. }

I want to check if LocalDateTime value from Game equals to the current LocalDateTime (LocalDateTime.now()) and if it does, do some action.
Of course I could use @Scheduled and call method every 1 hour, but I wonder if what I am describing is feasible.

Short summary:

Game entity in database with LocalDateTime = 2020-04-05 21:00:00

Current datetime: 2020-04-05 12:00:00 (do nothing, because current datetime doesn't equal to Game datetime)

[9 hours later]

Game entity in database (is immutable) with LocalDateTime = 2020-04-05 21:00:00

Current datetime: 2020-04-05 21:00:00 (current datetime matches with Game's datetime, so do some action (for example System.out.println(game))).

答案1

得分: 0

我建议使用注解@Scheduled来安排一个定时任务,该任务将使用Spring Data JPA处理与数据库相关的数据。

  1. @Scheduled(fixedRate=0 * * * *)
  2. @Transactional
  3. public void scheduleGameJob() {
  4. Optional<Game> game = gameRepo.findById(1L);
  5. if (game.isPresent()) {
  6. LocalDateTime gameTime = game.get().getLocalDateTime();
  7. if (gameTime.equals(LocalDateTime.now())) {
  8. //执行某些操作(例如 System.out.println(game))。
  9. }
  10. }
  11. }
英文:

I would suggest to schedule a job with annotation @Scheduled, which will process data with database using spring data jpa.

  1. @Scheduled(fixedRate=0 * * * *)
  2. @Transactional
  3. public void scheduleGameJob() {
  4. Optional&lt;Game&gt; game = gameRepo.findById(1L);
  5. if (game.isPresent()) {
  6. LocalDateTime gameTime = game.get().getLocalDateTime();
  7. if (gameTime.equals(LocalDateTime.now())) {
  8. //do some action (for example System.out.println(game))).
  9. }
  10. }
  11. }

huangapple
  • 本文由 发表于 2020年4月6日 00:25:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61045722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定