String sql = "SELECT * FROM customers"; Query query = entityManager.createNativeQuery(sql, Customer.class); Listcustomers = query.getResultList();
String sql = "UPDATE customers SET name = 'John Doe' WHERE id = 1"; Query query = entityManager.createNativeQuery(sql); entityManager.getTransaction().begin(); int rowsAffected = query.executeUpdate(); entityManager.getTransaction().commit();In this example, a native SQL query is created for updating a record in the 'customers' table. The query is executed using executeUpdate() method and the number of rows affected is returned. The javax.persistence.EntityManager class and createNativeQuery() method are part of the Java Persistence API (JPA) library, which is included in the javax.persistence package.