【问题标题】:Hibernate list using cache? -Not updating entity attribute使用缓存的休眠列表? - 不更新实体属性
【发布时间】:2012-01-14 06:42:24
【问题描述】:

我有一个使用休眠的应用程序。 我做了以下事情:

  1. 使用 List 列出数据库中的一些实体
  2. 手动登录我的Mysql数据库并更新了一些字段 实体
  3. 在休眠中再次使用 List 执行与 1 相同的查询

hibernate 列出的实体未更新。 如果我关闭并打开应用程序。然后它会显示正确更新的实体。

hibernate 是否默认使用某种缓存?

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/XXX</property>
<property name="hibernate.connection.username">XXXXXXXXXX</property>
<property name="hibernate.connection.password">XXXXXXXXXX</property>
<property name="show_sql">true</property>

列出实体的代码:

        Session s = HibernateUtil.getSession();
        Criteria c = s.createCriteria(Solicitacao.class, "s");
        //Add some Restrictions here
        List<Solicitacao> ls = c.list();
        s.close();

我的会话工厂:

    public class HibernateUtil {
    private static SessionFactory   sessionFactory  = null;
    static {
        // Configurações iniciais de caminho dos diretórios e arquivos
        URL url = HibernateUtil.class.getProtectionDomain().getCodeSource().getLocation();
        File myFile = null;
        try {
            myFile = new File(url.toURI());
        } catch (Exception ex) {

        }
        File dir = myFile.getParentFile();
        File xml = new File(dir, "hibernate.cfg.xml");
        /*
         * sessionFactory = new AnnotationConfiguration() .configure("br/com/netradiobrasil/pcp/" +
         * "hibernate/hibernate.cfg.xml") .buildSessionFactory();
         */
        sessionFactory = new AnnotationConfiguration().configure(xml).buildSessionFactory();
    }

    public static Session getSession() {
        return sessionFactory.openSession();
    }
}

我尝试在 hibernate.cfg.xml 中添加这些行

<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

也试过用:session.setCacheMode(CacheMode.IGNORE)

但还是没有解决我的问题

【问题讨论】:

    标签: mysql hibernate caching


    【解决方案1】:

    让我猜猜

    执行后

    Session s = HibernateUtil.getSession();
    Criteria c = s.createCriteria(Solicitacao.class, "s");
    //Add some Restrictions here
    List<Solicitacao> ls = c.list();
    

    您手动更改了数据库中的条目,然后重新运行查询?如果是,那么您可以关闭会话然后重新运行您的代码吗?

    【讨论】:

    • 对不起,我确实在第一次查询后关闭了会话......然后我创建了另一个会话并重新运行了相同的查询
    【解决方案2】:

    在我的 hibernate.cfg.xml 中添加这些行 - 启用 c3p0 解决了我的问题

    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">40</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">100</property>
    

    【讨论】:

      猜你喜欢
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2010-11-26
      • 2011-03-16
      • 1970-01-01
      • 2015-01-09
      • 2020-02-16
      相关资源
      最近更新 更多