Get the latest data for a list of objects

We use jpa with Toplink as an implementation and came up with a problem with updating entity lists.

This is basically a scenario:

private List<Entity> findAll()
{
    final String sql = "SELECT e from " + anEntityClass.getSimpleName() + " e";
    final Query query = itsManager.createQuery(sql);
    List<Entity> result = query.getResultList();

    return result;
}

But if we change the database by external means, the second call to the findAll () method will return the outdated information, since it reuses the information stored in the cache.

One solution to this problem is to indicate

query.setHint("toplink.refresh", "True");

Thus, we always get updated data. But then we are dependent on Toplink, and we will have to face a problem if we need to change suppliers.

I know that there is an entityManager.refesh () method, but I only saw it in combination with entitytManager.find () to get only one object.

Is there a standard way to get fresh data for a list of objects?

+3
3

persistence.xml .

<persistence-unit name="local" transaction-type="RESOURCE_LOCAL">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <properties>
        <property name="toplink.cache.type.default" value="NONE" />
        <property name="toplink.cache.type.the.class.package.ClassName" value="NONE" />
    </properties>
</persistence-unit>

. http://www.oracle.com/technology/products/ias/toplink/JPA/essentials/toplink-jpa-extensions.html#TopLinkCaching.

+1

, , . , , - .

.

EntityManager.clear(), ( )

0

JPA, , , . , , , , . refresh() - . , , , .

But I have to say that the pragmatic side of me says that you are unlikely to change your sustainability provider if you have something that works with TopLink, and then just continue with it. Learn how to make the best use of the tools you have and don’t worry about the chance “someday” that you need to switch to Hibernate or something else.

0
source

Source: https://habr.com/ru/post/1725991/


All Articles