public void testMetamodelCriteriaUpdate() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
      getPersistenceUnitServerSession()
          .logMessage(
              "Test simpleUpdate skipped for this platform, "
                  + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
      return;
    }
    EntityManager em = createEntityManager();
    int nrOfEmps =
        ((Number)
                em.createQuery("SELECT COUNT(e) FROM Employee e where e.firstName is not null")
                    .getSingleResult())
            .intValue();

    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);

    // test query "UPDATE Employee e SET e.firstName = 'CHANGED' where e.firstName is not null";
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(entityEmp_);
    cq.set(root.get(entityEmp_.getSingularAttribute("firstName", String.class)), "CHANGED");
    cq.where(qb.isNotNull(root.get(entityEmp_.getSingularAttribute("firstName"))));

    beginTransaction(em);
    try {
      Query q = em.createQuery(cq);
      int updated = q.executeUpdate();
      assertEquals(
          "simpleCriteriaUpdateTest: wrong number of updated instances", nrOfEmps, updated);

      // check database changes
      int nr =
          ((Number)
                  em.createQuery("SELECT COUNT(e) FROM Employee e WHERE e.firstName = 'CHANGED'")
                      .getSingleResult())
              .intValue();
      assertEquals(
          "simpleCriteriaUpdateTest: unexpected number of changed values in the database",
          nrOfEmps,
          nr);
    } finally {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }
      closeEntityManager(em);
    }
  }
  // test ejbqlString = "Update Employee e set e.lastName = case when e.firstName = 'Bob' then
  // 'Jones' when e.firstName = 'Jill' then 'Jones' else '' end";
  public void testMetamodelComplexConditionCaseInCriteriaUpdate() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
      getPersistenceUnitServerSession()
          .logMessage(
              "Test complexConditionCaseInUpdateTest skipped for this platform, "
                  + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
      return;
    }
    EntityManager em = createEntityManager();
    List<Employee> results = null;

    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);

    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(Employee.class);
    Case caseExp = qb.selectCase();
    caseExp.when(
        qb.equal(root.get(entityEmp_.getSingularAttribute("firstName", String.class)), "Bob"),
        "Jones");
    caseExp.when(
        qb.equal(root.get(entityEmp_.getSingularAttribute("firstName", String.class)), "Jill"),
        "Jones");
    caseExp.otherwise("");
    cq.set(root.get(entityEmp_.getSingularAttribute("lastName")), caseExp);

    beginTransaction(em);
    try {
      clearCache();

      em.createQuery(cq).executeUpdate();

      String verificationString = "select e from Employee e where e.lastName = 'Jones'";
      results = em.createQuery(verificationString).getResultList();
    } finally {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }
      closeEntityManager(em);
    }
    assertTrue("complexConditionCaseInUpdateTest - wrong number of results", results.size() == 2);
    for (Employee e : results) {
      assertTrue(
          "complexConditionCaseInUpdateTest wrong last name for - " + e.getFirstName(),
          e.getLastName().equals("Jones"));
    }
  }
  public String changePassword(String a, String b, String c) {

    //  em.getTransaction().begin();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaUpdate<UsersAchat> updateCriteria = cb.createCriteriaUpdate(UsersAchat.class);
    Root<UsersAchat> root = updateCriteria.from(UsersAchat.class);
    // update dateOfBirth property
    updateCriteria.set(root.get("password"), sha256(b));
    // set where clause
    System.out.println(a);
    updateCriteria.where(cb.equal(root.get("userid"), a));
    // update
    int affected = em.createQuery(updateCriteria).executeUpdate();
    HttpSession session =
        (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    session.invalidate();
    return "/Login?faces-redirect=true";

    // em.getTransaction().commit();
  }
  public void testMetamodelCriteriaUpdateEmbeddedField() {
    if ((getPersistenceUnitServerSession()).getPlatform().isSymfoware()) {
      getPersistenceUnitServerSession()
          .logMessage(
              "Test updateEmbeddedFieldTest skipped for this platform, "
                  + "Symfoware doesn't support UpdateAll/DeleteAll on multi-table objects (see rfe 298193).");
      return;
    }

    EntityManager em = createEntityManager();
    int nrOfEmps =
        ((Number)
                em.createQuery("SELECT COUNT(e) FROM Employee e where e.firstName is not null")
                    .getSingleResult())
            .intValue();

    Metamodel metamodel = em.getMetamodel();
    EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
    javax.persistence.metamodel.EmbeddableType<EmploymentPeriod> embedEmpPeriod_ =
        metamodel.embeddable(EmploymentPeriod.class);

    Calendar startCalendar = Calendar.getInstance();
    startCalendar.set(1905, 11, 31, 0, 0, 0);
    java.sql.Date startDate = new java.sql.Date(startCalendar.getTime().getTime());

    // em.createQuery("UPDATE Employee e SET e.period.startDate=
    // :startDate").setParameter("startDate", startDate).executeUpdate();
    CriteriaBuilder qb = em.getCriteriaBuilder();
    CriteriaUpdate<Employee> cq = qb.createCriteriaUpdate(Employee.class);
    Root<Employee> root = cq.from(entityEmp_);
    cq.set(
        root.get(entityEmp_.getSingularAttribute("period", EmploymentPeriod.class))
            .get(embedEmpPeriod_.getSingularAttribute("startDate", java.sql.Date.class)),
        startDate);

    beginTransaction(em);
    try {
      clearCache();

      int updated = em.createQuery(cq).executeUpdate();
      assertEquals(
          "testCriteriaUpdateEmbeddedField: wrong number of updated instances", nrOfEmps, updated);

      // check database changes
      int nr =
          ((Number)
                  em.createQuery(
                          "SELECT COUNT(e) FROM Employee e WHERE e.period.startDate = :startDate")
                      .setParameter("startDate", startDate)
                      .getSingleResult())
              .intValue();
      assertEquals(
          "testCriteriaUpdateEmbeddedField: unexpected number of changed values in the database",
          nrOfEmps,
          nr);
    } finally {
      if (isTransactionActive(em)) {
        rollbackTransaction(em);
      }
      closeEntityManager(em);
    }
  }