title: Proper Way to layer Spring JPA based DAO using Spring Boot Framework
body: <p>Am new to Spring Boot &amp; JPA...</p>
  
  <p>Let's say I have two entities mapped to two tables which are joined in a database.</p>
  
  <p>Student-1------&lt;-Course</p>
  
  <p>Also, lets presume that the database is already created and populated.</p>
  
  <p>This depicts that one student has many courses...</p>
  
  <p>My Student Entity:</p>
  
  <pre><code>@Entity
  public class Student {
  
      @OneToMany(mappedBy="student")
      private List&lt;Courses&gt; courses;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "Student_Id")
      private long studentId;
  
      @Column(name = "Student_Name")
      private String studentName;
  
      protected Student() { }
  
      // Getters &amp; Setters
  }
  </code></pre>
  
  <p>My Course Entity:</p>
  
  <pre><code>@Entity
  public class Course {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "Course_Id")
      private long courseId;
  
      @Id
      @Column(name = "Student_Id")
      private long studentId;
  
      @ManyToOne
      @PrimaryKeyJoinColumn(name="Student_Id", referencedColumnName="Student_Id")
      private Student student;
  
      @Column(name = "Course_Name")
      private String courseName;
  
      // Getters &amp; Setters
  
  }
  </code></pre>
  
  <p>In Spring Boot's Tutorial Guides, it illustrates how to extend a CrudRepository interface, but
  it doesn't specify how to setup a Spring based DAO which contains custom finder methods which use HQL and EntityManager inside it.</p>
  
  <p>Is the following DAO and DaoImpl correct?</p>
  
  <pre><code>public interface CourseDao {
      List&lt;Course&gt; findCoursesByStudentName(String studentName);
  }
  
  @Repository
  public class CourseDaoImpl implements CourseDao {
  
      @PersistenceContext
      EntityManager em;
  
      public List&lt;Course&gt; findCoursesByStudentName(String studentName) {
          String sql = "select c.courseName" +
                       "from Course c, Student s " +
                       "where c.course_id = s.student_id " +
                       "and s.studentName = :studentName ";
  
          Query query = em.createQuery(sql);
          query.setParameter("studentName", studentName);
          return query.getResultList();
      }
  }   
  </code></pre>
  
  <p>And then in the client code, for example, in the main class:</p>
  
  <pre><code> public class Application {
  
      @Autowired
      CustomerDao dao;
  
      public static void main (String args []) {
          List&lt;Course&gt; courses = dao.findCoursesByStudentName("John");
      }   
   }
  </code></pre>
  
  <p>Is this the standard way to use HQL inside Spring DAOs ? I've seend examples of the @Transactional annotation being prepended to the DAO class's impl (e.g. CustomerDAOImpl) ?</p>
  
  <p>Please let me know if this is the write way to structure my Spring Boot app or am I supposed to extend / add to the CrudRepository only?</p>
  
  <p>If someone could correct my example and point me to a URL which talks about HQL using Entities that are joined, I would be very grateful.</p>
  
  <p>The Spring Boot guides didn't depict joins or DAOs - I just need to learn how to properly create finder methods which emulate select statement which return lists or data structures.</p>
  
  <p>Thanks for taking the time to read this...</p>