title: Why em.persist() has no effect on database table?
body: <p>I have a RESTful service called AuthenticationEndpoint, which checks the users username/password pairs, creates token, save token to database and returns token to application client.
  Code works without errors and exceptions. 
  UserEJB bean should save token to <em>Token</em> database table and AuthenticationEJB bean should update token field in User database table. But as I see in MySQL Workbench nothing is saved or updated.
  UserEJB.getUsers() method work fine and return list of users and it seems that EJB container found persistence-unit <em>ForthDynamicWebProject</em>, but why I can't save and update entities? Thanks for your attention.</p>
  
  <p><strong>AuthenticationEndpoint RESTful service</strong></p>
  
  <pre><code>@ApplicationScoped
  @Path("/authentication")
  public class AuthenticationEndpoint {
  
      @Inject
      AuthenticateEJB authenticateEJB;
  
      @POST
      @Produces(MediaType.APPLICATION_JSON)
      @Consumes(MediaType.APPLICATION_JSON)
      public Response authenticateUser(String jsonString) {
          Gson gson = new Gson();
          Credentials credentials = gson.fromJson(jsonString, Credentials.class);
          String userName = credentials.getUserName();
          String password = credentials.getPassword();
  
          try {
              User user = authenticateEJB.checkUser(userName, password);
              if ((user instanceof User) &amp;&amp; user != null) {
                  return Response.ok().header("token", user.getToken().getToken()).build();
              } else {
                  return Response.status(Response.Status.FORBIDDEN).build();
              }
          } catch (Exception e) {
              return Response.status(Response.Status.FORBIDDEN).build();
          }
      }
  }
  </code></pre>
  
  <p>Here is the code of EJBs:</p>
  
  <p><strong>AuthenticateEJB</strong></p>
  
  <pre><code>@LocalBean
  @Stateless
  public class AuthenticateEJB {
  
  @PersistenceContext(unitName = "ForthDynamicWebProject")
  EntityManager em;
  
  @Inject
  UserEJB userEJB;
  
  public User checkUser(String userName, String password) {
      List&lt;User&gt; users = userEJB.getUsers();
  
      for (User user : users) {
          if (user.getUsername().equals(userName) &amp;&amp; user.getPassword().equals(password)) {
              user.setToken(userEJB.updateToken());
              em.merge(user);
              return user;
          }
      }
      return null;
  }
  }
  </code></pre>
  
  <p><strong>UserEJB</strong></p>
  
  <pre><code>@LocalBean
  @Stateless
  public class UserEJB {
  
  @PersistenceContext(unitName = "ForthDynamicWebProject")
  EntityManager em;
  
  @Inject
  Token token;
  
  public List&lt;User&gt; getUsers() {
      Query query = em.createQuery("from User");
      return (List&lt;User&gt;) query.getResultList();
  }
  
  public Token updateToken() {
      em.persist(token);
      return token;
  }
  }
  </code></pre>
  
  <p><strong>persistence.xml:</strong></p>
  
  <pre><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="ForthDynamicWebProject" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
      &lt;jta-data-source&gt;jdbc/second_attempt_hibernate&lt;/jta-data-source&gt;
      &lt;class&gt;converters.ConverterLocalDateTime&lt;/class&gt;
      &lt;class&gt;entity.DocStatus&lt;/class&gt;
      &lt;class&gt;entity.DocType&lt;/class&gt;
      &lt;class&gt;entity.Document&lt;/class&gt;
      &lt;class&gt;entity.Employee&lt;/class&gt;
      &lt;class&gt;entity.MailOrder&lt;/class&gt;
      &lt;class&gt;entity.MailOrderStatus&lt;/class&gt;
      &lt;class&gt;entity.Token&lt;/class&gt;
      &lt;class&gt;entity.User&lt;/class&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><strong>Token entity</strong></p>
  
  <pre><code>@Entity
  @Table(name = "tokens")
  @NamedQuery(name = "Token.findAll", query = "SELECT t FROM Token t")
  public class Token implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private int id;
  
  @Temporal(TemporalType.TIMESTAMP)
  private Date loginTime;
  
  private String token;
  
  // bi-directional one-to-one association to User
  @OneToOne(mappedBy = "token")
  private User user;
  
  public Token() {
      ConverterLocalDateTime converter = new ConverterLocalDateTime();
      loginTime = converter.convertToDatabaseColumn(LocalDateTime.now());
      token = Double.toString(Math.random());
  }
  
  *getters and setters are not showed*
  </code></pre>
  
  <p><strong>User entity</strong></p>
  
  <pre><code>@Entity
  @NamedQuery(name="User.findAll", query="SELECT u FROM User u")
  public class User implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy=GenerationType.IDENTITY)
      private int id;
  
      private String password;
  
      private String username;
  
      //bi-directional one-to-one association to Employee
      @OneToOne(mappedBy="user")
      private Employee employee;
  
      //bi-directional one-to-one association to Token
      @OneToOne
      @JoinColumn(name="tokID")
      private Token token;
  
      public User() {
      }
  
  *getters and setters are not showed*
  </code></pre>