2013-11-20 17 views
0

我使用Hibernate作爲服務器守護程序的ORM。休眠沒有看到從外部應用程序插入的數據

如果在運行時關聯的表是空的,但是在冬眠之外添加了一個項目,則此代碼不會返回任何項目。

public static List<QueueItem> queue() 
{ 
    SessionFactory factory = HibernateUtil.getSessionFactory(); 
    Session session = factory.openSession(); 


    session.flush(); 
    session.clear(); 

    @SuppressWarnings("unchecked") 
    List<QueueItem> topList = session.createQuery("FROM QueueItem i ORDER BY i.id asc").setMaxResults(3).list(); 

    session.close(); 
    return topList; 
} 

這裏是hibernate的配置

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
    <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://10.10.1.4/lanjukebox_test</property> 
    <property name="hibernate.connection.username">ussqldev</property> 
    <property name="hibernate.connection.password">ussqldev</property> 

    <property name="hibernate.jdbc.batch_size">25</property> 

    <property name="connection.pool_size">1</property> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="current_session_context_class">thread</property> 


    <property name="hibernate.cache.use_second_level_cache">false</property> 

    <property name="show_sql">false</property> 

    <property name="hbm2ddl.auto">validate</property> 

    <!-- List of XML mapping files --> 
    <mapping resource="usagisoft/lan/jukebox/domain/entities/Song.hbm.xml"/> 
    <mapping resource="usagisoft/lan/jukebox/domain/entities/Id3.hbm.xml"/> 
    <mapping resource="usagisoft/lan/jukebox/domain/entities/QueueItem.hbm.xml"/> 
    <mapping resource="usagisoft/lan/jukebox/domain/entities/Vote.hbm.xml"/> 
    <mapping resource="usagisoft/lan/jukebox/domain/entities/User.hbm.xml"/> 

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

如果我創建一個對象,並從它工作正常,如果已經有表中的數據在應用程序中堅持它,我插入它找到的數據其他對象,只要表在運行時爲空,就會出現問題。

這是一個很大的問題,因爲另一個應用程序處理插入這些對象的數據。

我在eclipse中使用hibernate 4.2.7.Final和Java 1.7(jdk1.7.0_45)。

+4

你有一個二級緩存?你確定其他應用程序已經做出了改變嗎? –

+0

那麼其他應用程序目前是MySQL Workbench,所以是的,如果我重新啓動守護進程並檢查它看到新記錄的表。 我將編輯我的休眠配置 –

+0

這可能會幫助你http://stackoverflow.com/questions/3827704/how-to-disable-hibernate-caching –

回答

-1

可以解決這個問題,因爲 的hibernate.cfg.xml

新增此標籤:

<property name="hibernate.connection.autocommit">true</property>