java - How to write a Sql query in Jpa repository? -
i trying display set of records database based on user's search request.i dont think there predefined method in jpa repository this.hence write query in repository this.
but during build, there exception stating "illegalargumentexception".
can me write proper query? if there better way of doing it, let me know.
the loanreport in argument object has user's search request
public interface loanreportrepository extends jparepository<loanreport, long> , jpaspecificationexecutor<loanreport> { public final static string get_loan_reports = "select * loan_report product=loanreport.product"; @query(get_loan_reports) list<loanreport> findbypreference(final loanreport loanreport); }
you can invoke jpql query this,
public interface loanreportrepository extends jparepository<loanreport, long> , jpaspecificationexecutor<loanreport> { public final static string get_loan_reports = "select lr loanreport lr product = :product"; @query(get_loan_reports) list<loanreport> findbypreference(@param("product") product);
here product
can value directly stored in db column or jpa entity. in latter case entity's identifier mapped loanreport
's foreign key constraints.
and can invoke findbypreference
passing product
property directly this.
loanreportrepository.findbypreference(loanreport.product);
see here more information. documentation pretty good.
p.s.
as far know there no built-in way pass object , expect all/some of property values mapped entity fields , db level columns.
another option use spring data jpa provided dynamic queries fields want search with. see here information
if want use multiple fields in search query, best option pass possible parameters method parameters , map them query's parameters. approach additionally allow sanitise or convert user parameter value different format may stored on db level.
Comments
Post a Comment