Creating CourseDAO and Controller

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 ...

Get Java EE 8 Development with Eclipse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.