书上说hibernate会在某些查询执行前同步数据库数据,但是我试了试,无论怎样查询,只要不commit,数据库都不会发生改变。到底会不会同步?最好能给个例子说明下。

解决方案 »

  1.   

    不 commit肯定不行,必须提交事务才会改变数据库,再用session.flush()刷新缓存
      

  2.   

    书上是这么说的:
    The synchronization of a persistence context with database is called flushing. Hibernate flushes occur at the following times:
    1. When a Transaction on the Hibernate API is committed
    2. Before a query is executed
    3. When the application calls session.flush() explicity对于第二点是不是我理解错了。我原来理解是把session的cache同步到数据库。现在看来好象是在说多个session的情况下,session1更新了数据库,session2的query在查询前会刷新session2的cache?
      

  3.   

    查了下资料,flush只是刷出sql语句,和commit并没有关系。
    多个session的交互涉及到锁定