euraad
euraad

Reputation: 2856

Delete on DATE TIME in Spring Boot JPA

I have a very slow code here. All it does is that it colleting all rows with a specific entity column.

List<Data> slectedData = dataService.findByJobNameOrderByDateTime(selectJob.getValue().getName());
List<Data> deleteThese = slectedData.subList(firstIndex - 1, lastIndex);
    for (List<Data> deleteTheseLists : Lists.partition(deleteThese, 2000)) {
        dataService.deleteInBatch(deleteTheseLists);
}

Then I create a sub list and I want to delete that sublist. But the sublist can be very large and then dataService.deleteInBatch(deleteTheseLists); takes alot of time.

The entity class Data looks like this:

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Data {

    // ID
    @Id
    @GeneratedValue
    private long id;

    // Made by
    private String jobName;
    private String calibrationName;
    @Column(columnDefinition = "DATETIME(3)")
    private LocalDateTime dateTime;

    // Analog input
    private float sa0;
    private float sa1;
    private float sa1d;

}

And I want to delete on dateTime. Is that possible in Spring Boot JPA? Because I can't delete on id because the id key is not correct indexed in the database because sometimes the database takes long time to insert data, and sometimes it goes fast. So it's a very unsecure way to delete entities in JPA.

Upvotes: 1

Views: 9284

Answers (1)

Ryuzaki L
Ryuzaki L

Reputation: 40078

Yes you can delete by dateTime using keywords

//equals
deleteByDateTimeEquals(LocalDateTime dateTime);

// lessthan
deleteByDateTimeLessThan(LocalDateTime dateTime);

// greaterthan
deleteByDateTimeGreaterThan(LocalDateTime dateTime);

Upvotes: 1

Related Questions