英文:
JPA event listener
问题
我需要构建一个从数据库获取记录的应用程序,这些记录等于某个值,如果返回 true,它将执行某些操作。我想知道 JPA 是否可以做到这一点。
我有一个实体 Game:
@Data
@Entity
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public Game() {
}
@OneToOne
private Team teamBlue;
@OneToOne
private Team teamRed;
@DateTimeFormat(pattern = "HH:mm:ss yyyy-MM-dd")
private LocalDateTime localDateTime;
}
以及返回 Game 的 GameRepository:
@Repository
public interface GameRepository extends JpaRepository<Game, Long> {
@Override
Optional<Game> findById(Long aLong);
}
我想要检查 Game 中的 LocalDateTime 值是否等于当前的 LocalDateTime(LocalDateTime.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:
@Data
@Entity
public class Game {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public Game() {
}
@OneToOne
private Team teamBlue;
@OneToOne
private Team teamRed;
@DateTimeFormat(pattern = "HH:mm:ss yyyy-MM-dd")
private LocalDateTime localDateTime;
}
and GameRepository which returns the Game:
@Repository
public interface GameRepository extends JpaRepository<Game, Long> {
@Override
Optional<Game> findById(Long aLong);
}
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处理与数据库相关的数据。
@Scheduled(fixedRate=0 * * * *)
@Transactional
public void scheduleGameJob() {
Optional<Game> game = gameRepo.findById(1L);
if (game.isPresent()) {
LocalDateTime gameTime = game.get().getLocalDateTime();
if (gameTime.equals(LocalDateTime.now())) {
//执行某些操作(例如 System.out.println(game))。
}
}
}
英文:
I would suggest to schedule a job with annotation @Scheduled, which will process data with database using spring data jpa.
@Scheduled(fixedRate=0 * * * *)
@Transactional
public void scheduleGameJob() {
Optional<Game> game = gameRepo.findById(1L);
if (game.isPresent()) {
LocalDateTime gameTime = game.get().getLocalDateTime();
if (gameTime.equals(LocalDateTime.now())) {
//do some action (for example System.out.println(game))).
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!

评论