How to mount a generic query using jpa nativequery

0

Consider the following table:

table: people_info

|| row || name || surname || nationality || time_do_coracao
| 1 | marcelo | aragao | brasil | sp
| 2 | maria | fonseca | brasil | sp
| 3 | joao | timotio | brasil | palmeiras
| 4 | marcelo | vasconcelos | brazil | corinthians

Imagine that I have a form with all these fields, where none of them is required. I need to take into account only the fields entered by the user, so if the user populates the field nationality = brazil and tipe_do_coracao = sp, the application should bring lines 1 and 2.

In my jpa application I did the following:

em.createNativeQuery ("select nome from info_pessoas where nome = ? and sobrenome = ? and nacionalidade = ? and time_do_coracao = ?")

obs: the "?" will be filled with the values entered by the user, in this case the 3 and 4 "?" were filled with Brazil and sp respectively.

The problem is that no line is being returned, what is the right way to mount the query?

    
asked by anonymous 16.01.2016 / 11:46

1 answer

0

Felipe,

When creating a nativeQuery think how you would do this directly in the database, ie, what works in your SQL will work in createNativeQuery. This works:

    em.createNativeQuery("select nome from info_pessoas where nacionalidade = 'Brasil' and time_do_coracao = 'SP'").getResultList();
    
16.01.2016 / 21:05