Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 在特定时间后自动删除表中的行-JPA Spring Boot_Database_Spring Boot_Jpa_Jdbc_Service - Fatal编程技术网

Database 在特定时间后自动删除表中的行-JPA Spring Boot

Database 在特定时间后自动删除表中的行-JPA Spring Boot,database,spring-boot,jpa,jdbc,service,Database,Spring Boot,Jpa,Jdbc,Service,有没有办法在SpringBoot中使用JPA实现一个服务,在一段时间后可以删除表中的一行?大约一周 还是会是一个非常非常慢的线程是不好的做法 @Setter @Getter @Entity public class ConferenceRoom { @Id @GeneratedValue private Long id; private long start; private long end; // Delete row after a week hav

有没有办法在SpringBoot中使用JPA实现一个服务,在一段时间后可以删除表中的一行?大约一周

还是会是一个非常非常慢的线程是不好的做法

@Setter
@Getter
@Entity
public class ConferenceRoom {
    @Id
    @GeneratedValue
    private Long id;
    private long start;
    private long end; // Delete row after a week have passed from time end
    private String email; // The owner who created the meeting
    private String members = ""; // e.g myMember@host1.com;mySecondMember@host2.com
}

您可以使用spring的计划任务

首先,用@EnableScheduling注释标记@Configuration类

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}
@EnableScheduling注释告诉Spring创建一个后台任务执行器。如果没有此注释,则不会安排任何内容

然后创建一个@Component类,开始创建要执行的方法,并将它们标记为@Scheduled


您可以使用Quartz之类的计划程序定期运行清理作业,以删除早于“X”的记录,或者使用它来计划作业,以便在将来删除特定记录。是要物理删除记录,还是只是不查看记录?在后一种情况下,可以在实体上指定@Where.@M.Deinum Intresting。所以我只需要在我的实体类中放置@Where?如果您使用的是hibernate,那么这应该是您所需要的。这样您就看不到它们了,它们仍然在数据库中,但是当您使用EntityManager查询数据库时,它们不会出现,如果您使用普通JDBC或SQL查询,它们会出现。
@Component
public class MySchedule {

    @Scheduled(cron = "0 0 12 * * FRI") // this method will be executed as 12:00:00 AM of every friday
    public void myMethod() {
        // do your logic
    }
}