title: EJB, JPA/Hibernate. I cannot make an insert in a database
body: <p>I am using Glassfish, EJB, JPA and Hibernate as a JPA implementation. I can get a select from a database via JPA but an insert does not work. Looks like a problem with JTA(may be a transaction was not completed or I use an another transaction) Persistence content does not flush to a database. The same code works properly when I use eclipselink as JPA implementation but I have to use hibernate.</p>
  
  <pre>
  <code>
  
      @Stateless
      @LocalBean
      public class BeanISManagedByContainer
      {
  
          @PersistenceContext(unitName = "com.company_JPATest2-ejb_ejb_1.0PU")
          EntityManager entityManager;
  
          public String getMessage(int id)
          {
              return entityManager.find(Message.class, id).getText();
          }
  
          public void addMessaage(String txt)
          {
              Message message = new Message();
              message.setText(txt);
              entityManager.persist(message);
          }
      }
  
  </code>
  </pre>
  
  <p>getMessage() method is works properly, but addMessaage() does not insert any data to a database and there are not any logs. Persistence context was not flushed to a database. I have tried to manage a transaction manually but the same result. I do not know what is wrong with a hibernate configuration. Please advise.</p>
  
  <pre>
  <code>
  
      @Stateless
      @LocalBean
      @TransactionManagement(TransactionManagementType.BEAN)
      public class ManualTransactions
      {
          @PersistenceContext(unitName = "com.company_JPATest2-ejb_ejb_1.0PU")
          EntityManager entityManager;
  
          @Resource
          private UserTransaction transaction;
  
          public void addMessaage(String txt) throws Exception
          {
              Message message = new Message();
              message.setText(txt);
  
              transaction.begin();
              Logger.getLogger(ManualTransactions.class.getName())
                      .info("transaction status: " + transaction.getStatus());
              entityManager.persist(message);
              Logger.getLogger(ManualTransactions.class.getName())
                      .info("transaction status: " + transaction.getStatus());
              transaction.commit();
              Logger.getLogger(ManualTransactions.class.getName())
                      .info("transaction status: " + transaction.getStatus());
          }
      }
  
  **logs:**
  Info:   transaction status: 0
  Info:   transaction status: 0
  Info:   transaction status: 6
  
  </code>
  </pre>
  
  <p><strong>persistance.xml</strong>
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
    &lt;persistence-unit name="com.company_JPATest2-ejb_ejb_1.0PU" transaction-type="JTA"&gt;
   &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
   &lt;jta-data-source&gt;jdbc/JPATestPool&lt;/jta-data-source&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
    &lt;property name="hibernate.show_sql" value="true"/&gt;
    &lt;property name="hibernate.format_sql" value="true"/&gt;
    &lt;property name="hibernate.use_sql_comments" value="true"/&gt;
   &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>pow.xml</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
      &lt;artifactId&gt;JPATest2&lt;/artifactId&gt;
      &lt;groupId&gt;com.company&lt;/groupId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
    &lt;/parent&gt;
  
      &lt;groupId&gt;com.company&lt;/groupId&gt;
      &lt;artifactId&gt;JPATest2-ejb&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;packaging&gt;ejb&lt;/packaging&gt;
  
      &lt;name&gt;JPATest2-ejb&lt;/name&gt;
  
      &lt;properties&gt;
          &lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
    &lt;dependency&gt;
     &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
     &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
     &lt;version&gt;5.2.12.Final&lt;/version&gt;
    &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax&lt;/groupId&gt;
              &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
              &lt;version&gt;7.0&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.7&lt;/source&gt;
                      &lt;target&gt;1.7&lt;/target&gt;
                      &lt;compilerArguments&gt;
                          &lt;endorseddirs&gt;${endorsed.dir}&lt;/endorseddirs&gt;
                      &lt;/compilerArguments&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-ejb-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;ejbVersion&gt;3.1&lt;/ejbVersion&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.6&lt;/version&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;phase&gt;validate&lt;/phase&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;copy&lt;/goal&gt;
                          &lt;/goals&gt;
                          &lt;configuration&gt;
                              &lt;outputDirectory&gt;${endorsed.dir}&lt;/outputDirectory&gt;
                              &lt;silent&gt;true&lt;/silent&gt;
                              &lt;artifactItems&gt;
                                  &lt;artifactItem&gt;
                                      &lt;groupId&gt;javax&lt;/groupId&gt;
                                      &lt;artifactId&gt;javaee-endorsed-api&lt;/artifactId&gt;
                                      &lt;version&gt;7.0&lt;/version&gt;
                                      &lt;type&gt;jar&lt;/type&gt;
                                  &lt;/artifactItem&gt;
                              &lt;/artifactItems&gt;
                          &lt;/configuration&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;</code></pre>
  </div>
  </div>
  </p>