我想更好地瞭解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登錄... 我如何使用休眠會話有什麼問題嗎?
在第一種情況下,您調用'getCurrentSession()',在第二種情況下調用'openSession()'。這是你的意圖嗎? – Rob
我第一次調用getCurrentSession並進行查詢。然後我關閉會話(是否正確?),並使新的查詢我必須調用openSession?對?爲什麼我得到了例外? – besmart