I'm trying to compare an entity before and after an update. The idea is to check whether a specific column has been updated and process that data. This is what I have so far:
@Entity
@EntityListeners(CarEntityListener.class)
public class Car {
@Column
private Long id;
@Column
private String status;
@Transient
private String loadedStatus;
}
class CarEntityListener {
@PostLoad
private void onLoad(Car car) {
car.setLoadedStatus(car.getStatus());
}
@PostUpdate
private void onUpdate(Car car) {
if (!car.getLoadedStatus().equals(car.getStatus()) {
// Do processing here
}
}
}
I use @Transient annotation to achieve this. But I was wondering if there is a better way to approach this, that basically gives me the previous and current object on update, something like this:
@PostUpdate
private void onUpdate(Car oldCar, Car newCar) {
if (!oldCar.getStatus().equals(newCar.getStatus()) {
// Do processing here
}
}
What are other approaches to address this scenario? I'm using Hibernate with SpringBoot.
Using Annotation @Transient is a very good approach and not approach better than this because if you have multiple joins on an entity and your query work for two entities and fail for one entity so it whole query data revert to the original without any update.