code: public Session currentSession() throws HibernateException {
          Object value = TransactionSynchronizationManager.getResource(sessionFactory);
          if (value instanceof Session) {
              return (Session) value;
          }
  
          if (value instanceof SessionHolder) {
              SessionHolder sessionHolder = (SessionHolder) value;
              Session session = sessionHolder.getSession();
              if (TransactionSynchronizationManager.isSynchronizationActive() && !sessionHolder.isSynchronizedWithTransaction()) {
                  TransactionSynchronizationManager.registerSynchronization(createSpringSessionSynchronization(sessionHolder));
                  sessionHolder.setSynchronizedWithTransaction(true);
                  // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
                  // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
                  FlushMode flushMode = session.getHibernateFlushMode();
                  if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                      session.setHibernateFlushMode(FlushMode.AUTO);
                      sessionHolder.setPreviousFlushMode(flushMode);
                  }
              }
              return session;
          }
  
          if (jtaSessionContext != null) {
              Session session = jtaSessionContext.currentSession();
              if (TransactionSynchronizationManager.isSynchronizationActive()) {
                  TransactionSynchronizationManager.registerSynchronization(createSpringFlushSynchronization(session));
              }
              return session;
          }
  
          if (allowCreate) {
              // be consistent with older HibernateTemplate behavior
              return createSession(value);
          }
  
          throw new HibernateException("No Session found for current thread");
      }
docs: Retrieve the Spring-managed Session for the current thread, if any.