How to update JPA entity cache when database gets modified by external processes?

I’m working with a MySQL database that has several tables and views. These views are basically joins between different tables. I created RESTful web services using these database objects and deployed them on a Tomcat server.

The problem is that there’s an external application that updates the database tables independently. When I query the database directly, I can see the updated data. But when I fetch data through my REST services, the old values are still returned. I think this happens because JPA caches the entity data and doesn’t know about the external changes.

I tried to solve this by adding some methods to my base facade class:

public abstract class BaseFacade<T> {
    private Class<T> entityType;
    private String entityTypeName;
    private static boolean needsRefresh = true;

    public static void triggerRefresh() { needsRefresh = true; }

    public BaseFacade(Class<T> entityType) {
        this.entityType = entityType;
        this.entityTypeName = entityType.getSimpleName();
    }

    private void performRefresh() {
        if (needsRefresh) {
            EntityManager manager = getEntityManager();
            manager.flush();

            for (EntityType<?> type : manager.getMetamodel().getEntities()) {
                if (type.getName().contains(entityTypeName)) {
                    try {
                        manager.refresh(type);
                        // success logging
                    }
                    catch (IllegalArgumentException ex) {
                        // error logging - usually says entity is not managed
                    }
                }
            }

            needsRefresh = false;
        }
    }
}

I call performRefresh() from my finder methods, but I keep getting IllegalArgumentException saying the entity is not managed.

How can I properly refresh my JPA entities to reflect the latest database changes made by external processes?

your refresh logic won’t work - you can’t refresh entitytype objects that way. try adding @Cacheable(false) to your entity classes or just turn off l2 cache completely. you could also call entityManager.detach() on the entities before pulling fresh data from the database.

You’re trying to refresh EntityType metadata instead of actual entity instances. The manager.refresh(type) call won’t work because EntityType is metadata, not a managed entity. I’ve hit this same issue when external processes modify database tables while JPA entities stay cached. Easiest fix is clearing the entire persistence context with entityManager.clear() before running queries that need fresh data. This forces JPA to reload entities from the database. For targeted cache invalidation, set up a second-level cache with programmatic eviction. Hibernate’s second-level cache lets you evict specific entity types using sessionFactory.getCache().evictEntityData(EntityClass.class). Better performance than clearing everything. You can also use native queries with result transformers when you need the latest data - this bypasses the JPA cache completely. Works great for read-only operations where you don’t need managed entities.

wait, you’re using hibernate as your jpa provider? have you thought about database triggers or a notification system? how often do these external updates happen? maybe polling with timestamp checks would work better than managing cache manually.