1

I'm trying to write a project that takes food with different labels (standing for different kind of food such as vegetables, canned, etc) and delete them automatically after given expiration time for each label for different food, passes. I've tried to implement it using @Schedule EJB but I've got confused understanding it since I'm still new to this. Here is the entity code:

@Id
@GeneratedValue
@Column(name = "ID", nullable = false)
private Integer id;
@Column(name = "LABEL", nullable = false)
private Integer label;
@NotBlank
@Length(min = 2, max = 255)
@Column(name = "FOODNAME", nullable = false)
private String foodName;
@Column(name = "FOODNUMBER", nullable = false)
private Integer foodNumber;

and the dependencies in it's class:

import org.hibernate.validator.constraints.NotBlank;
import javax.persistence.*;

and the reference for @Schedule:

EJB @Schedule wait until method completed

thanks for any help in advance.

zloster
  • 1,149
  • 11
  • 26
reza naderii
  • 15
  • 12

1 Answers1

1

Add a column expires to the table. Fill in the timestamp when row expires when you insert the row.

Now you can add a @Schedule method which searches for rows in the table where the current time is after the value in the column expires. If there are any, you can delete them.

Or execute the query delete from Food where expires < :now and pass the current time for the parameter now. Then the database will do the query and deletion but you can't log which rows were deleted which might help debugging problems.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820