Let's now create the CourseDAO class. We will have an instance of JPAEntityFactoryBean injected (auto-wired) into this class. Create the packt.jee.course_management_jpa.dao package and the CourseDAO class in it:
@Component public class CourseDAO { @Autowired JPAEntityFactoryBean entityFactoryBean; public List<Course> getCourses() { //Get entity manager EntityManagerFactory emf = entityFactoryBean.getEntityManagerFactory(); EntityManager em = emf.createEntityManager(); //Execute Query TypedQuery<Course> courseQuery = em.createNamedQuery("Course.findAll", Course.class); List<Course> courses = courseQuery.getResultList(); em.close(); return courses; } }
In the getCourses method, we first create EntityManager ...