I use spring @Version for optimistic lock, and I use @Caching to cache the jpa entity. the version in database was auto incremented when the transaction was committed, but the version in cache was not incremented. How could I keep them consistent whenever the transaction is committed or rollback?

Comment From: snicoll

I assume you meant @Cacheable. The cache abstraction is a straightforward arrangement where objects returned from the method is called. The optimistic locking is the least of your problem here as it would store the whole graph and not a single entity and that will lead to serve you outdated data.

There is a warning in the doc about this.

You should use the second level cache of your JPA provider for this. If you have more questions, please follow-up on StackOverflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements.