'Saveing changes to injected Repository in EntityListener

Hey guys I would like to update a second repository when a change on another repository is made. Unfortunally the changes aren't saved.

My EntityListener: (source for injecting into EntityListener: https://stackoverflow.com/a/12241344/12648501)

@Component
public class CarListener {

    static private VersionRepository versionRepository ;

    @Autowired
    public void init(VersionRepository versionRepository ) {
        CarListener.versionRepository = versionRepository ;
        log.info("Initializing with dependency [" + versionRepository + "]");
    }

    @PostPersist
    @Transactional
    public void onPostAction(Car o) {

        //gets the Version from injected repo, increments it and saves() it
        Version version = versionRepository.getVersionById("CAR_VERSION");
        version.setVersionNumber(version.getVersionNumber() + 1);
        versionRepository .save(version);
    }
}

When I initialize the db with a repo.saveAll(List) I can log those increments and it seems fine. But when I call repo.save() on a single Car the increment is back to the start where I initialized it (Before and after I call save(). The Reference of the repo is still the same but somehow those changes seem to never getting saved. Thanks for your help

Entities:

@Entity
@Data
@EntityListeners(value = CarListener.class)
public class Car {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;
    ...
    }
@Entity
@Data
public class Version {
    @Id
    private String versionType;

    private long versionNumber;
}


Sources

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

Source: Stack Overflow

Solution Source