2015-11-13 50 views
2

我想更好地瞭解Hibernate的工作方式......休眠 - 第二查詢給出未知服務請求

我有一個問題,我解決不了。

應用程序啓動時,它使一個查詢

Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
session.beginTransaction(); 
int result; 
String query = "SELECT count(*) as posti_disponibili from occupazione t inner join "; 
query += "(select id_posto_park, max(date_time) as MaxDate from occupazione group by id_posto_park) tm on "; 
query += "t.id_posto_park = tm.id_posto_park and t.date_time = tm.Maxdate and t.isOccupied = 0"; 

BigInteger bi = (BigInteger) session.createSQLQuery(query).uniqueResult(); 
result = bi.intValue(); 
HibernateUtil.shutdown(); 

最後我關閉當前會話。

然後,之後,我有第二個查詢來完成:

我打開一個新的會話

Session session = HibernateUtil.getSessionFactory().openSession(); 
session.beginTransaction(); 
Client client = new Client(); 
client.setIdClient(clientId); 
String queryString ="from it.besmart.models.Client where clientId = :c)"; 
List<?> list = session.createQuery(queryString).setProperties(client).list(); 

(第一個是與方法HibernateUtil.shutdown();關閉),但我得到了,現在,

org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.cache.spi.RegionFactory] 
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:184) 
at org.hibernate.cfg.Settings.getRegionFactory(Settings.java:300) 
at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1322) 
at org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:677) 
at it.besmart.parkserver.SocketClientHandler.run(SocketClientHandler.java:78) 
at java.lang.Thread.run(Thread.java:744) 

我不明白爲什麼,我關上了第一屆會議,但後來開了一家新的..

它是正確的關閉每個查詢

編輯 我試圖解決這一問題的會議,但沒有結果。

現在我有第一個選擇查詢,這很順利。這是在應用程序的啓動。

try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 

    String query = "SELECT count(*) as posti_disponibili from occupazione t inner join "; 
    query += "(select id_posto_park, max(date_time) as MaxDate from occupazione group by id_posto_park) tm on "; 
    query += "t.id_posto_park = tm.id_posto_park and t.date_time = tm.Maxdate and t.isOccupied = 0"; 

    BigInteger bi = (BigInteger) session.createSQLQuery(query).uniqueResult(); 
    result = bi.intValue(); 
} 

我沒有提交或刷新它。 然後,應用往上走,我有第二個查詢,所以我的getCurrentSession並嘗試做選擇

Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
session.beginTransaction(); 
Client client = new Client(); 
client.setIdClient(clientId); 
String queryString ="from it.besmart.models.Client c where c.clientId = :c"; 
logger.debug(queryString); 
// logger.debug(session); 

Query theQuery = session.createQuery(queryString).setProperties(client); 
List<?> list = theQuery.list(); 

應用程序停止,沒有出來,我不知道發生了什麼也要去,因爲我無法設置休眠與pi4j登錄... 我如何使用休眠會話有什麼問題嗎?

+0

在第一種情況下,您調用'getCurrentSession()',在第二種情況下調用'openSession()'。這是你的意圖嗎? – Rob

+0

我第一次調用getCurrentSession並進行查詢。然後我關閉會話(是否正確?),並使新的查詢我必須調用openSession?對?爲什麼我得到了例外? – besmart

回答

2

如果您使用sessionFactory.getCurrentSession(),您將獲得一個「當前會話」,該會話綁定到事務的生命週期,並且在事務結束(提交或回滾)時自動刷新並關閉。

如果您決定使用sessionFactory.openSession(),則必須自己管理會話並手動刷新並關閉它。

欲瞭解更多信息,請登錄Hibernate transactions

+1

謝謝@Abdelhak我讀了文檔,但我不明白一件事。您表示該事務在刷新或提交後關閉。我只有SELECT查詢,所以不需要提交。這是否意味着我必須始終使用相同的會話,即使是以下查詢也是如此? – besmart