2012-10-23 85 views
2

我正在Hibernate上工作。我正在測試一個簡單的Java,在兩個單獨的會話中加載2本書的實體。我加載了第一本書對象,讓主線程睡眠5秒,在此期間,我在數據庫中更改了書的名稱。之後,第二次會話將再次加載該書。但這本書的名字還是一樣的。當它預計也會改變。Java Hibernate對象不刷新

請幫忙。我編寫了一個servlet程序,但具有相同的問題,更新後的數據不會在db更改後顯示。

這裏是我的代碼:

import java.util.Date; 
import java.util.HashSet; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Set; 

import org.hibernate.CacheMode; 
import org.hibernate.Hibernate; 
import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 

import org.hibernate.stat.Statistics; 

/** 
* @author Guruzu 
* 
*/ 
public class Launch_3_5 { 

    private static SessionFactory sessionFactory; 

    public static Session getSession() { 
     if (sessionFactory == null) { 
      sessionFactory = new Configuration().configure() 
        .buildSessionFactory(); 
     } 
     Session hibernateSession = sessionFactory.openSession(); 
     return hibernateSession; 
    } 

    public static SessionFactory getSessionFactory() { 
     if (sessionFactory == null) { 
      sessionFactory = new Configuration().configure() 
        .buildSessionFactory(); 
     } 
     return sessionFactory; 
    } 

    public static void main(String[] args) { 
     Session session1 = getSessionFactory().openSession(); 

     try { 

      Transaction tx = session1.beginTransaction(); 

      Book book1 = (Book) session1.get(Book.class, (long) 1); 

      System.out.println("Book: " + book1.getName()); 


     // session1.flush(); 
     } finally { 

      session1.close(); 
     } 

     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Session session2 = getSessionFactory().openSession(); 

     try { 

      Book book2 = (Book) session2.get(Book.class, (long) 1); 
      System.out.println("Book: " + book2.getName()); 

     } finally { 

      session2.close(); 
     } 

    } 
} 

Book6_1.hbm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
    <class name="Book" table="Book6_1"> 

     <id name="book_id" type="long" column="BOOK_ID"> 
      <generator class="increment"> 
      </generator> 
     </id> 
     <property name="isbn" type="string"> 
      <column name="ISBN" length="50" not-null="true" unique="true" /> 
     </property> 
     <property name="name" type="string"> 
      <column name="BOOK_NAME" length="100" not-null="true" /> 
     </property> 
     <property name="publishDate" type="date" column="PUBLISH_DATE" /> 
     <property name="price" type="int" column="PRICE" /> 


    </class> 
</hibernate-mapping> 

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetutorial</property> 
     <property name="hibernate.connection.username">root</property> 
     <property name="hibernate.connection.password">root</property> 
     <property name="hibernate.connection.pool_size">10</property> 
     <property name="show_sql">true</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 

     <mapping resource="Book_6_1.hbm.xml" /> 

    </session-factory> 
</hibernate-configuration> 

回答

1

你的第二屆會議的擊球second-level cache。如果你不想這樣做,你可以完全在你的配置文件(use_second_level_cache = false)或映射文件(從hbm.xml中刪除'cache'元素)中關閉緩存。

或者你可以改變你的查詢使用經常去數據庫CacheMode.REFRESH

Session session2 = getSessionFactory().openSession(); 
    session2.setCacheMode(CacheMode.REFRESH); 
    try { 
     Book book2 = (Book) session2.get(Book.class, (long) 1); 
    } finally { 
     session2.close(); 
    } 
+0

嗨Sharakan,感謝您的幫助,其實我並沒有使用第2緩存,我忘了發帖時註釋掉是我的碼。對於那個很抱歉。我編輯了我的問題。請再看看並提供幫助。 Hibernate查詢數據庫2次,但結果仍然是相同的。看起來問題在於我管理會話的方式。 – limitcalm

+0

我現在在代碼或映射文件中看不到任何問題。所以你的SQL日誌顯示兩個select查詢,兩種形式都是'select ... from Book6_1 where BOOK_ID = 1'?如果接下來要嘗試的是確保您的更新已提交。如果不是,第二個選擇將不會看到更新。您甚至可以嘗試在單獨的查詢工具中更新後運行選擇,以確保提交的數據庫狀態符合您的預期。 – sharakan