title: hibernate, mysql, glassfish v3, and JTA datasource
body: <p>I'm attempting to use hibernate entity manager with mysql and glassfish.  I'm getting the following error when attempting to use a JTA datasource: </p>
  
  <pre><code>Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
          at org.hibernate.impl.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:376)
          at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1367)
          at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:858)
          at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:733)
          ... 37 more
  </code></pre>
  
  <p>Here is how I have configured my persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;
    &lt;persistence-unit name="myPU" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;jta-data-source&gt;jdbc/mysql&lt;/jta-data-source&gt;
      &lt;class&gt;com.my.shared.entity.MyFile&lt;/class&gt;
      &lt;class&gt;com.my.shared.entity.MyRole&lt;/class&gt;
      &lt;class&gt;com.my.shared.entity.MyUser&lt;/class&gt;
      &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="hibernate.hbm2ddl.auto" value="create-drop"/&gt;
        &lt;property name="hibernate.show.sql" value="true" /&gt;
      &lt;/properties&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>However, when I configure a non-jta datasource, it works fine</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;
        &lt;persistence-unit name="myPU" transaction-type="JTA"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;non-jta-data-source&gt;jdbc/mysql&lt;/non-jta-data-source&gt;
          &lt;class&gt;com.my.shared.entity.MyFile&lt;/class&gt;
          &lt;class&gt;com.my.shared.entity.MyRole&lt;/class&gt;
          &lt;class&gt;com.my.shared.entity.MyUser&lt;/class&gt;
          &lt;exclude-unlisted-classes&gt;true&lt;/exclude-unlisted-classes&gt;
          &lt;properties&gt;
            &lt;property name="hibernate.hbm2ddl.auto" value="create-drop"/&gt;
            &lt;property name="hibernate.show.sql" value="true" /&gt;
          &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>That's all well and good, but I would really like to use:</p>
  
  <pre><code>em.persist(myObject);
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>em.getTransaction().begin();
  em.persist(myObject);
  em.getTransaction().commit();
  </code></pre>
  
  <p>
  </p>
  
  <p>Am I missing something with the hibernate configuration, or is it even possible to use a JTA datasource?</p>