Hibernate - the second query gives Unknown service requested
Asked Answered
A

1

3

I'm trying to understand better how Hibernate works...

I've a problem I cannot resolve.

When the application starts, it makes a query

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();

At the end I close the current session.

Then, after it, I have a second query to be accomplished:

I open a new session (the first one was closed with the method 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();

but I got, now,

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)

I cannot understand why, I closed the first session, but then opened a new one..

Is it correct to close the session on each query

EDIT I'm trying to solve this problem, but with no result.

Now I have the first select query, which goes well. It's at the startup of the application.

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();
}

I do not commit or flush it. Then, going up with the application, I have the second query, so I getCurrentSession and try to do the select

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();

The application stops, nothing comes out, I don't know what's going on also because I cannot setup hibernate to log with pi4j... Is there something wrong in how I use hibernate sessions?

Antevert answered 13/11, 2015 at 17:51 Comment(2)
In the first case, you call getCurrentSession() and in the second case you call openSession(). Is this what you intended?Cerberus
I call getCurrentSession the first time and the query goes. Then I close the session (is it correct?) and to make the new queries I have to call openSession? Right? And why I got the exception?Antevert
S
2

If you use sessionFactory.getCurrentSession(), you'll obtain a "current session" which is bound to the lifecycle of the transaction and will be automatically flushed and closed when the transaction ends (commit or rollback).

If you decide to use sessionFactory.openSession(), you'll have to manage the session yourself and to flush and close it "manually".

For more info go to Hibernate transactions.

Swingletree answered 13/11, 2015 at 18:34 Comment(1)
Thank you @Swingletree I read the documentation, but I cannot understand a thing. You said that the transaction is closed after a flush or a commit. I have just SELECT queries, so no commit is needed. Does it means I have to use always the same session, even for the following queries?Antevert

© 2022 - 2024 — McMap. All rights reserved.