EntityManager entityManager = Persistence.createEntityManagerFactory("myPersistenceUnit").createEntityManager(); entityManager.getTransaction().begin(); Query query = entityManager.createQuery("UPDATE Employee e SET e.salary = :newSalary WHERE e.id = :employeeId"); query.setParameter("newSalary", 50000); query.setParameter("employeeId", 1234); int recordsUpdated = query.executeUpdate(); entityManager.getTransaction().commit();
EntityManager entityManager = Persistence.createEntityManagerFactory("myPersistenceUnit").createEntityManager(); entityManager.getTransaction().begin(); Query query = entityManager.createQuery("DELETE FROM Employee e WHERE e.department = :department"); query.setParameter("department", "Sales"); int recordsDeleted = query.executeUpdate(); entityManager.getTransaction().commit();In this example, we use `executeUpdate()` to delete all `Employee` records that belong to the "Sales" department. The method returns the number of records that were deleted in the database, which we can use to verify that the operation was successful. Package Library: `javax.persistence` is part of the Java Persistence API (JPA) standard, which is included in the Java EE (Enterprise Edition) library. It is also available as a standalone package through the `javax.persistence` Maven artifact.