Hibernate JPA + MySQL:无法在createNativeQuery中禁用缓存

4
我正在使用Hibernate/JPA和MySQL,因为遗留原因,在某个时候使用createNativeQuery。该应用程序与使用相同数据库的不同服务器一起工作,因此它不应进行任何缓存,而应始终显示最新结果。我通过在数据库编辑器中手动更改值来模拟其他服务器,但更改后它总是给出旧结果。
据我所知,我应禁用任何第二级缓存(不太重要,因为我不使用任何ORM对象),清除任何第一级缓存,并禁用mysql查询缓存(已在数据库级别上完成)。我哪里做错了,或者我忘记了什么?这让我发疯。
init(): servlet的开始
    entityFactory = Persistence.createEntityManagerFactory("persistence-id");

getEntityManager(): 每个请求的开始

    destroyEntityManager(); // just in case
    entityFactory.getCache().evictAll();
    entityManager = entityFactory.createEntityManager();
    entityManager.setProperty("javax.persistence.cache.storeMode",
            CacheStoreMode.BYPASS);
    entityManager.clear(); // just in case

destroyEntityManager(): 结束每个请求

    if (entityManager != null) {
        if (entityManager.getTransaction().isActive()) {
            entityManager.flush();
            entityManager.getTransaction().commit();
        }
        entityManager.clear();
        if (entityManager.isOpen()) {
            entityManager.close();
        }
        entityManager = null;
    }

destroy(): servlet 结束

    destroyEntityManager();
    if (entityFactory != null) {
        entityFactory.close();
    }

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="WallMountBackOffice-PU">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>...</class>
    <class>...</class>
    <properties>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.connection.url" value="jdbc:mysql://localhost/ourschema" />
        <property name="hibernate.connection.username" value="root" />
        <property name="hibernate.connection.password" value="" />
        <property name="hibernate.connection.pool_size" value="10" />
        <property name="hibernate.connection.autocommit" value="false" />
        <property name="hibernate.connection.release_mode" value="on_close" />
        <property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.cache.use_second_level_cache"
            value="false" />
        <property name="hibernate.cache.use_query_cache" value="false" />
        <property name="javax.persistence.sharedCache.mode" value="NONE" />
        <property name="org.hibernate.cacheable" value="false" />
    </properties>
</persistence-unit>

执行“select ...” 的代码:

    ...
    Query jpaQuery = entityManager.createQuery(query);
    entityManager.getTransaction().begin();
    jpaQuery.executeUpdate();
    entityManager.getTransaction().commit();
2个回答

3

第一行有一个错误,应该是“BYPASS”,不是“REFRESH”,如下所示:

query.setHint("javax.persistence.cache.retrieveMode", "BYPASS");

建议使用JPA枚举而不是字符串字面量,这样写:

query.setHint(QueryHints.CACHE_RETRIEVE_MODE, CacheRetrieveMode.BYPASS);
query.setHint(QueryHints.CACHE_STORE_MODE, CacheStoreMode.REFRESH); 

1
您可以使用setHint() storeModeretrieveMode方法。如果您正在尝试检索记录,请使用带有BYPASSretrieveMode
对于Hibernate
query.setHint("javax.persistence.cache.storeMode", "REFRESH");

query.setHint("javax.persistence.cache.retrieveMode", "REFRESH"); 

针对 EclipseLink。

query.setHint("javax.persistence.cache.storeMode", "REFRESH"); 

query.setHint("javax.persistence.cache.retrieveMode", "REFRESH"); 

JPA 2.0 规范

public enum CacheRetrieveMode {

    /**
     * Read entity data from the cache: this is
     * the default behavior.
     */
    USE,

    /**
     * Bypass the cache: get data directly from
     * the database.
     */
    BYPASS
}

public enum CacheStoreMode {

    /**
     * Insert/update entity data into cache when read
     * from database and when committed into database:
     * this is the default behavior. Does not force refresh
     * of already cached items when reading from database.
     */
    USE,

    /**
     * Don't insert into cache.
     */
    BYPASS,

    /**
     * Insert/update entity data into cache when read
     * from database and when committed into database:
     * Forces refresh of cache for items read from database.
     */
    REFRESH
}   

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接