If there’s one thing you have to understand to successfully use JPA (Java Persistence API) it’s the concept of a Cache. Almost everything boils down to the Cache at one point or another.

Here’s a quick cheat sheet of the JPA world:

  • A Cache is a copy of data, copy meaning pulled from but living outside the database.
  • Flushing a Cache is the act of putting modified data back into the database.
  • A PersistenceContext is essentially a Cache. It also tends to have it’s own non-shared database connection.
  • An EntityManager represents a PersistenceContext (and therefore a Cache)
  • An EntityManagerFactory creates an EntityManager (and therefore a PersistenceContext/Cache)

本质上,ORM框架关注的是数据库里的数据和它们的缓存(内存对象)之间的交互。

文章里讲的cache主要指的是“一级缓存(first level cache)”。