Hibernate禁用查询缓存不工作

时间:2016-08-29 13:28:09

标签: hibernate jpa

Hibernate禁用查询缓存无法正常工作, 每当我进行查询时,搜索都会返回先前的查询数据。 我尝试了几个互联网的例子,但没有一个对我有用。

我已经尝试过这个配置。

    <properties>

        <!-- Nao remover AutoReconect=true -->
        <property name="javax.persistence.jdbc.url"
            value="jdbc:mysql://localhost:3307/teste?autoReconnect=true" />
        <property name="javax.persistence.jdbc.user" value="teste" />
        <property name="javax.persistence.jdbc.password" value="teste" />
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />

        <!-- validate | update | create | create-drop -->
        <property name="connection.useUnicode" value="true" />
        <property name="connection.characterEncoding" value="uf-8" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />

        <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" />

        <!-- C3P0 -->
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
        <property name="connection.provider_class"
            value="org.hibernate.connection.C3P0ConnectionProvider" />

        <property name="hibernate.c3p0.validate" value="true" />
        <property name="hibernate.c3p0.acquire_increment" value="5" />
        <property name="hibernate.c3p0.min_size" value="5" />
        <property name="hibernate.c3p0.max_size" value="40" />
        <property name="hibernate.c3p0.max_statements" value="0" />
        <property name="hibernate.c3p0.testConnectionOnCheckout"
            value="true" />
        <property name="hibernate.c3p0.preferredTestQuery" value="SELECT 1" />
        <property name="hibernate.c3p0.idle_test_period" value="100" />

    </properties>

</persistence-unit>

在我的JPAUtil类

public class JpaUtil {

private static final EntityManagerFactory emf;
private static final ThreadLocal<EntityManager> threadLocal;

static {
    emf = Persistence.createEntityManagerFactory("simuladosPU");
    emf.getCache().evictAll();
    threadLocal = new ThreadLocal<EntityManager>();
}

public static EntityManager getEntityManager() {
    EntityManager em = threadLocal.get();
    if (em == null) {
        em = emf.createEntityManager();
        threadLocal.set(em);
    }
    return em;
}

public static void closeEntityManager() {
    EntityManager em = threadLocal.get();
    if (em != null) {
        em.close();
        threadLocal.set(null);
    }
}

我需要做些什么来禁用此查询缓存?

1 个答案:

答案 0 :(得分:0)

只需使用查询提示。

使用命名查询的示例: em.createNamedQuery(&#34; MyLocation.myExampleNamedQuery&#34;)。setHint(QueryHints.CACHE_USAGE,CacheUsage.DoNotCheckCache).getResultList()。get(0);

通过结果和好运告诉我们。