title: Hibernate 4.2.2 The Network Adapter could not establish the connection
body: <p>I've been working through several Hibernate Tutorial but the application won't run. I get a JDBCConnectionException.</p>
  
  <p>I think maybe there is something wrong with the connection settings: "ERROR: I/O-Fehler: The Network Adapter could not establish the connection"</p>
  
  <p>Therefore I linked the SQL Developer Settings and my hibernate.cfg.xml.</p>
  
  <p><a href="http://i.stack.imgur.com/NISmM.jpg" rel="nofollow">Link to SQL Developer Settings</a></p>
  
  <pre><code> &lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:oracle:thin:@localhost:1521:xe&lt;/property&gt;
          &lt;property name="connection.username"&gt;test&lt;/property&gt;
          &lt;property name="connection.password"&gt;test&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt; org.hibernate.dialect.Oracle10gDialect&lt;/property&gt;
  
          &lt;mapping resource="TEST.hbm.xml"/&gt;
  
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>Here is the Main Class:</p>
  
  <pre><code>import org.hibernate.HibernateException;
  import org.hibernate.SessionFactory;
  import org.hibernate.Session;
  import org.hibernate.Query;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.metadata.ClassMetadata;
  import org.hibernate.service.ServiceRegistry;
  import org.hibernate.service.ServiceRegistryBuilder;
  import java.util.Map;
  
  
  public class Main {
      private static final SessionFactory ourSessionFactory;
      private static final ServiceRegistry serviceRegistry;
  
      static {
          try {
              Configuration configuration = new Configuration();
              configuration.configure();
  
              serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
              ourSessionFactory = configuration.buildSessionFactory(serviceRegistry);
          } catch (Throwable ex) {
              throw new ExceptionInInitializerError(ex);
          }
      }
  
      public static Session getSession() throws HibernateException {
          return ourSessionFactory.openSession();
      }
  
      public static void main(final String[] args) throws Exception {
          final Session session = getSession();
  
  
          final Test a1 = new Ankuendigung(3,"AA", "AAAA");
          session.beginTransaction();
          session.save(a1);
          session.getTransaction().commit();
  
          try {
              System.out.println("querying all the managed entities...");
              final Map metadataMap = session.getSessionFactory().getAllClassMetadata();
              for (Object key : metadataMap.keySet()) {
                  final ClassMetadata classMetadata = (ClassMetadata) metadataMap.get(key);
                  final String entityName = classMetadata.getEntityName();
                  final Query query = session.createQuery("from " + entityName);
                  System.out.println("executing: " + query.getQueryString());
                  for (Object o : query.list()) {
                      System.out.println("  " + o);
                  }
              }
          } finally {
              session.close();
          }
      }
  }
  </code></pre>
  
  <p>Here is the complete stacktrace:</p>
  
  <pre><code>   Apr 19, 2016 11:31:22 AM org.hibernate.annotations.common.Version &lt;clinit&gt;
  INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
  Apr 19, 2016 11:31:22 AM org.hibernate.Version logVersion
  INFO: HHH000412: Hibernate Core {4.2.2.Final}
  Apr 19, 2016 11:31:22 AM org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: HHH000206: hibernate.properties not found
  Apr 19, 2016 11:31:22 AM org.hibernate.cfg.Environment buildBytecodeProvider
  INFO: HHH000021: Bytecode provider name : javassist
  Apr 19, 2016 11:31:22 AM org.hibernate.cfg.Configuration configure
  INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
  Apr 19, 2016 11:31:22 AM org.hibernate.cfg.Configuration getConfigurationInputStream
  INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
  Apr 19, 2016 11:31:22 AM org.hibernate.cfg.Configuration addResource
  INFO: HHH000221: Reading mappings from resource: Ankuendigung.hbm.xml
  Apr 19, 2016 11:31:22 AM org.hibernate.cfg.Configuration doConfigure
  INFO: HHH000041: Configured SessionFactory: null
  Apr 19, 2016 11:31:22 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
  Apr 19, 2016 11:31:22 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000115: Hibernate connection pool size: 20
  Apr 19, 2016 11:31:22 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000006: Autocommit mode: false
  Apr 19, 2016 11:31:22 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000401: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:xe]
  Apr 19, 2016 11:31:22 AM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000046: Connection properties: {user=vias, password=****}
  Apr 19, 2016 11:31:25 AM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
  WARN: HHH000342: Could not obtain connection to query metadata : I/O-Fehler: The Network Adapter could not establish the connection
  Apr 19, 2016 11:31:25 AM org.hibernate.dialect.Dialect &lt;init&gt;
  INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
  Apr 19, 2016 11:31:25 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
  INFO: HHH000422: Disabling contextual LOB creation as connection was null
  Apr 19, 2016 11:31:25 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
  INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
  Apr 19, 2016 11:31:25 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory &lt;init&gt;
  INFO: HHH000397: Using ASTQueryTranslatorFactory
  Apr 19, 2016 11:31:27 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  WARN: SQL Error: 17002, SQLState: 08006
  Apr 19, 2016 11:31:27 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  ERROR: I/O-Fehler: The Network Adapter could not establish the connection
  Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection
      at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:132)
      at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
      at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
      at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
      at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
      at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
      at Main.main(Main.java:39)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
  Caused by: java.sql.SQLRecoverableException: I/O-Fehler: The Network Adapter could not establish the connection
      at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:489)
      at oracle.jdbc.driver.PhysicalConnection.&lt;init&gt;(PhysicalConnection.java:553)
      at oracle.jdbc.driver.T4CConnection.&lt;init&gt;(T4CConnection.java:254)
      at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
      at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
      at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
      at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
      at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
      ... 10 more
  Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
      at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:439)
      at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:454)
      at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:693)
      at oracle.net.ns.NSProtocol.connect(NSProtocol.java:251)
      at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1140)
      at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:340)
      ... 17 more
  Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:149)
      at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
      at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:405)
      ... 22 more
  
  Process finished with exit code 1
  </code></pre>
  
  <p>Thanks a lot!</p>