Create a Dynamic Query in Spring -


i have been using standard jpa implementation while , had no problems creating dynamic queries resulting algorithms search users in database. however, moving spring framework (an older version - 3.2, unfortunately) , cannot figure out how create dynamic query in spring.

with javax.persistance.entitymanager, call createquery , give string work with. however, have found in spring can use following code define query in annotation.

@repository @suppresswarnings("unused") public interface personrepository extends jparepository<person, long>, crudrepository<person, long> {      @override     list<person> findall(sort sort);      @override     list<person> findall();      @query("select p person p order p.lname asc, p.fname asc, p.mname asc")     list<person> findallsort();      @query("select p person p upper(p.username) = upper(?1)")     person findpersonbyusername(string username);   } 

here simplest dynamic query example give replicate spring:

public list<person> getpersons(list<long> perids) {     list<person> persons;     string whereclause = "";     (int = 0; < perids.size(); i++) {         if (i != 0)             whereclause += " or ";         whereclause += "p.perid = '" + perids.get(i) + "'";     }     persons = em.createquery("select p person p " + whereclause).getresultlist();     return persons; } 

maybe better question here ask if possible or if should keep implementation using entity manager. being said, recomend me change code using entitymanager on using spring framework?

spring allows use @repository, not force so. spring offers nice interfacing of jpa separates low level concerns (datasource definition, , transaction management) high level ones (dao, declarative transactions).

there chapter in spring framework reference manual jpa. should read part transaction management in previous chapters.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -