hibernate - Proper Way to layer Spring JPA based DAO using Spring Boot Framework -
am new spring boot & jpa...
let's have 2 entities mapped 2 tables joined in database.
student-1------<-course
also, lets presume database created , populated.
this depicts 1 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, illustrates how extend crudrepository interface, doesn't specify how setup spring based dao contains custom finder methods use hql , entitymanager inside it.
is following dao , 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 in client code, example, in main class:
public class application { @autowired customerdao dao; public static void main (string args []) { list<course> courses = dao.findcoursesbystudentname("john"); } }
is standard way use hql inside spring daos ? i've seend examples of @transactional annotation being prepended dao class's impl (e.g. customerdaoimpl) ?
please let me know if write way structure spring boot app or supposed extend / add crudrepository only?
if correct example , point me url talks hql using entities joined, grateful.
the spring boot guides didn't depict joins or daos - need learn how create finder methods emulate select statement return lists or data structures.
thanks taking time read this...
if understood question correct have 2 questions:
- how create dao , daoimpl?
- where put transaction annotations?
in regards first question want point out question in regards spring-data-jpa
using hibernate jpa provider, not spring-boot
.
using spring data skip create dao directly use custom repository extending standard 1 crudrepository
. in case don't have write more code than:
@repository public interface studentrepository extends crudrepository<student, long> { list<student> findbystudentname(string studentname); }
which sufficient , spring data take care of filling correct implementation if use
@autowired studentrepository studentrepo;
in service class. annotate methods @transactional
make sure working expected.
in regards question hql please spring data jpa documentation, points out of cases should sufficient stick proper named methods in interface or go named queries (section 3.3.3) or use @query
annotation (section 3.3.4) manually define query, e.g. should work (didn't tried):
@repository public interface @courserepository extends crudrepository<course, long> { @query("select c.coursename course c, student s c.course_id = s.student_id , s.studentname = :studentname") public list<course> findcoursesbystudentname(string studentname); }
Comments
Post a Comment