mardi 31 mars 2015

Proper Way to layer Spring JPA based DAO using Spring Boot Framework

Am new to Spring Boot & JPA...


Let's say I have two entities mapped to two tables which are joined in a database.


Student-1------<-Course


Also, lets presume that the database is already created and populated.


This depicts that one student has many courses...


My Student Entity:



@Entity
public class Student {

@OneToMany(mappedBy="student")
private List<Courses> courses;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Student_Id")
private long studentId;

@Column(name = "Student_Name")
private String studentName;

protected Student() { }

// Getters & Setters
}


My Course Entity:



@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 & Setters

}


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.


Is the following DAO and DaoImpl correct?



public interface CourseDao {
List<Course> findCoursesByStudentName(String studentName);
}

@Repository
public class CourseDaoImpl implements CourseDao {

@PersistenceContext
EntityManager em;

public List<Course> 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();
}
}


And then in the client code, for example, in the main class:



public class Application {

@Autowired
CustomerDao dao;

public static void main (String args []) {
List<Course> courses = dao.findCoursesByStudentName("John");
}
}


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) ?


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?


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.


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.


Thanks for taking the time to read this...


Aucun commentaire:

Enregistrer un commentaire