title: using Hibernate and MS SQL with intelliJ IDEA
body: <p>I am using Hibernate/JPA 2.0 with SQL Server and IntelliJ IDEA here's a sample code I made to test the connection:</p>
  
  <pre><code>public class App {
      public static final String SELECT_QUERY = "select u from UsersEntity as u where u.userId = :userId";
  
      public static void main(String[] args)
      {
          String userId = "1";
          PersistenceProvider persistenceProvider = new HibernatePersistence();
          EntityManagerFactory entityManagerFactory = persistenceProvider.createEntityManagerFactory("newPersistenceUnit", new HashMap());
          EntityManager entityManager = entityManagerFactory.createEntityManager();
  
          List&lt;UsersEntity&gt; users = entityManager.createQuery(SELECT_QUERY, UsersEntity.class).setParameter("userId", userId).getResultList();
          System.out.println(users);
          entityManager.close();
      }
  }
  </code></pre>
  
  <p>And here's what I receive in the console:</p>
  
  <pre><code>"C:\Program Files\Java\jdk1.6.0_29\bin\java" -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 12.0.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.6.0_29\jre\lib\alt-rt.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\alt-string.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\jce.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\resources.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\rt.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.6.0_29\jre\lib\ext\sunpkcs11.jar;D:\mobitrack\out\production\web;D:\mobitrack\lib\javax.persistence.jar;D:\mobitrack\lib\hibernate-entitymanager-4.2.0.Final.jar;D:\mobitrack\lib\jboss-logging-3.1.0.GA.jar;D:\mobitrack\lib\hibernate-core-4.2.0.Final.jar;D:\mobitrack\lib\antlr-2.7.7.jar;D:\mobitrack\lib\jboss-transaction-api_1.1_spec-1.0.0.Final.jar;D:\mobitrack\lib\dom4j-1.6.1.jar;D:\mobitrack\lib\hibernate-jpa-2.0-api-1.0.1.Final.jar;D:\mobitrack\lib\javassist-3.15.0-GA.jar;D:\mobitrack\lib\hibernate-commons-annotations-4.0.1.Final.jar;D:\mobitrack\lib\tools.jar;D:\mobitrack\lib\sqljdbc4-4.0.2206.100.jar;C:\Program Files\JetBrains\IntelliJ IDEA 12.0.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.mobitrack.services.App
  26 mars 2013 17:44:18 org.hibernate.annotations.common.Version &lt;clinit&gt;
  INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
  26 mars 2013 17:44:18 org.hibernate.Version logVersion
  INFO: HHH000412: Hibernate Core {4.2.0.Final}
  26 mars 2013 17:44:18 org.hibernate.cfg.Environment &lt;clinit&gt;
  INFO: HHH000206: hibernate.properties not found
  26 mars 2013 17:44:18 org.hibernate.cfg.Environment buildBytecodeProvider
  INFO: HHH000021: Bytecode provider name : javassist
  26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
  26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000115: Hibernate connection pool size: 20
  26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000006: Autocommit mode: true
  26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;databaseName=MOBITRACKDB]
  26 mars 2013 17:44:19 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
  INFO: HHH000046: Connection properties: {user=sa, password=****, autocommit=true, release_mode=auto}
  </code></pre>
  
  <p>When debugging I found that it stops at:</p>
  
  <pre><code>EntityManagerFactory entityManagerFactory = persistenceProvider.createEntityManagerFactory("newPersistenceUnit", new HashMap());
  </code></pre>
  
  <p>Knowing that "newPersistenceUnit" is the name of my persistence unit.</p>
  
  <p><strong>Edit:</strong>
  Yes it works outside IDEA. And when I use the console in IDEA to query the data it works.</p>
  
  <p>I tried using the next code:</p>
  
  <pre><code>public class HibernateUtil {
  
  private static final SessionFactory sessionFactory;
  
  static {
      try {
  
          sessionFactory = new Configuration().configure().buildSessionFactory();
      } catch (Throwable ex) {
  
          System.err.println("Initial SessionFactory creation failed." + ex);
          throw new ExceptionInInitializerError(ex);
      }
  }
  
  public static SessionFactory getSessionFactory() {
      return sessionFactory;
  }
  
  }
  </code></pre>
  
  <p>I receive the same as above and the debugging stops at :</p>
  
  <pre><code>sessionFactory = new Configuration().configure().buildSessionFactory();
  </code></pre>
  
  <p><strong>Edit2:</strong></p>
  
  <p>I used an EntityManager:</p>
  
  <pre><code>EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("newPersistenceUnit");
  
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      entityManager.getTransaction().begin();
      List&lt;AdminsEntity&gt; result = entityManager.createQuery( "from AdminsEntity",     AdminsEntity.class ).getResultList();
      for ( AdminsEntity event : result ) {
          logged = event;
      }
      entityManager.getTransaction().commit();
      entityManager.close();
      return logged;
  </code></pre>
  
  <p>It stops at:</p>
  
  <pre><code>    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("newPersistenceUnit");
  </code></pre>
  
  <p>here is my persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"&gt;
  &lt;persistence-unit name="newPersistenceUnit"&gt;
      &lt;class&gt;com.mobitrack.entities.GroupsEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.ZonesEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.AdminsEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.AlarmsEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.LocationsEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.UsersEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.UserGroupsEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.UserZonesEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.SettingsEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.MessagesEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.ProfilesEntity&lt;/class&gt;
      &lt;class&gt;com.mobitrack.entities.ProfileZonesEntity&lt;/class&gt;
      &lt;properties&gt;
          &lt;property name="hibernate.connection.url" value="jdbc:sqlserver://localhost:1433;databaseName=MOBITRACKDB"/&gt;
          &lt;property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/&gt;
          &lt;property name="hibernate.connection.username" value="admin"/&gt;
          &lt;property name="hibernate.connection.password" value="password"/&gt;
          &lt;property name="hibernate.query.factory_class"
                    value="org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory"/&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>Any ideas?</p>