sql - generic query using jpa -


consider following table:

|row |name    |last_name   |nacionality |team   |1   |adam    |ritch       |usa         |titans   |2   |scoth   |buffalo     |usa         |titans   |3   |jhon    |veron       |usa         |islanders   |4   |dylan   |schdit      |usa         |bulls   

imagine form these fields (name, last_name, nacionality , team) none field mandatory. need take account fields user informed, for example: if user informed nacionality = usa , team = titans aplication need retrieve rows 1 , 2.

in jpa application did:

 em.createnativequery ("select name info_people name = ? , last_name = ? , nacionality = ? , team = ?") 

remark: "?" filled values inserted user, in case, 3 , 4 "?" filled values usa , titans respectively.

but no row returned, wrong query?

i agree @jb nizet comment, indeed can create query dynamically based on user's request, can design query consider null values, not need dynamic query build.

you can accomplish checking if values null or equal column's value. here's example:

select name info_people   (? null or ? = name)    , (? null or ? = last_name)    , (? null or ? = nacionality)    , (? null or ? = team) 

also note parameters set null before calling query, otherwise not work expected.

good luck!


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -