'@Version annotation not increment in one to many relationship

I have problem with @Version annotation. I have ParkingEntity with field and annotation @Version.

public class ParkingEntity {
@Id
@Column(name = "parking_id")
private String idParking;
private String name;
private String address;
private int numberOfParkingSlots;
private int numberOfChargers;
private boolean isLPGAllowed;
private Double widthOfSlot;
@OneToMany(mappedBy= "parking")
private Set<CarEntity> cars = new HashSet<>();
@Version
private Integer version;

When I am doing update for that entity, column version increments, it works correctly, but when I want to put CarEntity to set of cars it fail. Column annotation does not change. I am adding and removing car in methods in ParkingEntity:

public void addCar(CarEntity carEntity) {
    cars.add(carEntity);
    carEntity.setParking(this);
}

public void removeCar(CarEntity carEntity) {
    cars.remove(carEntity);
    carEntity.setParking(null);
}

I can not get why it does not works. I tried with parkingRepository.saveAndFlush but the problem still exists.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source