EntityManager em = entityManagerFactory.createEntityManager(); Employee employee = em.find(Employee.class, 1); if(employee != null) { System.out.println("Employee name is " + employee.getName()); } em.close();
EntityManager em = entityManagerFactory.createEntityManager(); Order order = em.find(Order.class, new OrderPK(1, "ABC")); if(order != null) { System.out.println("Order status is " + order.getStatus()); } em.close();In this example, we are using the find method to retrieve an Order object with composite primary key consisting of an integer and a string value, and printing the status of the order if it exists. The javax.persistence package library contains the classes and interfaces for the Java Persistence API, and can be found in the javax.persistence jar file.