EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit"); EntityManager em = emf.createEntityManager();
Employee employee = new Employee(); employee.setName("John Smith"); employee.setSalary(35000); em.getTransaction().begin(); em.persist(employee); em.getTransaction().commit();
TypedQueryIn this example, we create a TypedQuery instance using the createQuery() method of the EntityManager. We then set a named parameter using the setParameter() method, and execute the query using the getResultList() method. The javax.persistence package is a part of the Java Persistence API (JPA) library, which is a standard for object-relational mapping (ORM) in Java applications.query = em.createQuery("SELECT e FROM Employee e WHERE e.salary > :minSalary", Employee.class); query.setParameter("minSalary", 40000); List results = query.getResultList();