java—为什么在spring scheduler中更新时,更改不会反映在数据库中?

velaa5lx  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(201)

我的更改不会反映在数据库中。我正在尝试对此tasksscheduler类进行更改。但数据库中没有反映任何更改。如果我这样做:

System.out.println(bannerRepo.save(banner))

然后显示输出。但是没有发生数据库更改。但若我尝试在调度程序类之外保存或删除,那个么就会发生更改。我的tasksscheduler类:

package com.shopKpr.apps.medicine.repositories.admin.jpa;

import com.shopKpr.apps.medicine.model.admin.Banner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
@EnableScheduling
public class TasksScheduler {

    private final BannerRepo bannerRepo;

    public TasksScheduler( BannerRepo bannerRepo ) {
        this.bannerRepo = bannerRepo;
    }

    //sec, min, hour
    @Scheduled(cron = "0 14 3 * * *", zone = "Asia/Dacca") //Schedule at night 1 AM
    public void schedulerTask() {
        updateOfferState();
    }

    @Transactional
    void updateOfferState() {

        java.sql.Date today = new java.sql.Date(new java.util.Date().getTime());

        List<Banner> bannerList = bannerRepo.findAll();

        bannerList.parallelStream()
                .filter(banner -> today.after(banner.getBannerStartDate()))
                .forEach(banner -> {
                    banner.setEnabled(true);
                    bannerRepo.save(banner);
                });

        List<Banner> deletableBanners = bannerList.stream()
                .filter(banner -> today.after(banner.getBannerEndDate()))
                .collect(Collectors.toList());

        deletableBanners.forEach(banner -> bannerRepo.deleteById(banner.getBannerId()));

    }
}

我的旗帜班:

package com.shopKpr.apps.medicine.model.admin;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@AllArgsConstructor
@NoArgsConstructor
@Data
@Table
@Entity
public class Banner implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long bannerId;
    @Column(nullable = false)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MMM dd, yyyy", timezone = "Asia/Dhaka")
    private Date bannerStartDate;
    @Column(nullable = false)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MMM dd, yyyy", timezone = "Asia/Dhaka")
    private Date bannerEndDate;
    @Column(nullable = false, columnDefinition = "TEXT")
    private String bannerImage;
    @Column(nullable = false)
    private boolean enabled;
    private String hyperLinkId;
}

横幅存储库:

package com.shopKpr.apps.medicine.repositories.admin.jpa;

import com.shopKpr.apps.medicine.model.admin.Banner;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BannerRepo extends JpaRepository<Banner, Long> {

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题