Get all the records of column in Spring Data JPA, even we specify it as parameter












0














There is a table called zipcode with columns as id, zip, city, state.



In our case, we want to get the records based on either id or zip or city or state.



The General SQL query looks like



SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;


If we want to get a specific state then, we will replace the state value with the given name.



SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';


If we want a specific city, then we can write



SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;


The problem with Spring Data JPA is, if we write a method



@Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)


I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.



How can we achieve this in Spring Data JPA, using native query ?










share|improve this question



























    0














    There is a table called zipcode with columns as id, zip, city, state.



    In our case, we want to get the records based on either id or zip or city or state.



    The General SQL query looks like



    SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;


    If we want to get a specific state then, we will replace the state value with the given name.



    SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';


    If we want a specific city, then we can write



    SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;


    The problem with Spring Data JPA is, if we write a method



    @Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
    List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)


    I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.



    How can we achieve this in Spring Data JPA, using native query ?










    share|improve this question

























      0












      0








      0







      There is a table called zipcode with columns as id, zip, city, state.



      In our case, we want to get the records based on either id or zip or city or state.



      The General SQL query looks like



      SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;


      If we want to get a specific state then, we will replace the state value with the given name.



      SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';


      If we want a specific city, then we can write



      SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;


      The problem with Spring Data JPA is, if we write a method



      @Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
      List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)


      I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.



      How can we achieve this in Spring Data JPA, using native query ?










      share|improve this question













      There is a table called zipcode with columns as id, zip, city, state.



      In our case, we want to get the records based on either id or zip or city or state.



      The General SQL query looks like



      SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state=state;


      If we want to get a specific state then, we will replace the state value with the given name.



      SELECT * FROM zipcode WHERE id=id AND zip=zip AND city=city AND state='California';


      If we want a specific city, then we can write



      SELECT * FROM zipcode WHERE id=id AND zip=zip AND city='Los Angles' AND state=state;


      The problem with Spring Data JPA is, if we write a method



      @Query(value = "SELECT * FROM zipcode WHERE id=?1 AND zip=?2 AND city=?3 AND state=?4", nativeQuery = true)
      List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)


      I want to get all the cities but a specific state only, then I can't use this method, like how I can use the above SQL query, where we just need to replace the column value.



      How can we achieve this in Spring Data JPA, using native query ?







      java mysql database spring spring-data-jpa






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      KC7

      8611




      8611
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:



          public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
          Zipcode zipcode = new Zipcode();
          if (id != null)
          zipcode.setId(id);
          if (zip != null)
          zipcode.setZip(zip);
          ...
          return zipcodeRepository.findAll(Example.of(zipcode));
          }





          share|improve this answer








          New contributor




          gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


























            0














            Try this,



            @Query(value = "SELECT * FROM zipcode WHERE  (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND  (city=?3 or ?3 is null)  AND (state=?4 or ?4 is null) ", nativeQuery = true)
            List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944169%2fget-all-the-records-of-column-in-spring-data-jpa-even-we-specify-it-as-paramete%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:



              public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
              Zipcode zipcode = new Zipcode();
              if (id != null)
              zipcode.setId(id);
              if (zip != null)
              zipcode.setZip(zip);
              ...
              return zipcodeRepository.findAll(Example.of(zipcode));
              }





              share|improve this answer








              New contributor




              gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.























                0














                I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:



                public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
                Zipcode zipcode = new Zipcode();
                if (id != null)
                zipcode.setId(id);
                if (zip != null)
                zipcode.setZip(zip);
                ...
                return zipcodeRepository.findAll(Example.of(zipcode));
                }





                share|improve this answer








                New contributor




                gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





















                  0












                  0








                  0






                  I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:



                  public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
                  Zipcode zipcode = new Zipcode();
                  if (id != null)
                  zipcode.setId(id);
                  if (zip != null)
                  zipcode.setZip(zip);
                  ...
                  return zipcodeRepository.findAll(Example.of(zipcode));
                  }





                  share|improve this answer








                  New contributor




                  gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  I don't know if Spring Data has a way of doing this in a single method, but you could try using an Example:



                  public List<Zipcode> getZipcodes(Integer id, Integer zip, String city, String state){
                  Zipcode zipcode = new Zipcode();
                  if (id != null)
                  zipcode.setId(id);
                  if (zip != null)
                  zipcode.setZip(zip);
                  ...
                  return zipcodeRepository.findAll(Example.of(zipcode));
                  }






                  share|improve this answer








                  New contributor




                  gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  share|improve this answer



                  share|improve this answer






                  New contributor




                  gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.









                  answered yesterday









                  gbandres

                  3939




                  3939




                  New contributor




                  gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.





                  New contributor





                  gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  gbandres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.

























                      0














                      Try this,



                      @Query(value = "SELECT * FROM zipcode WHERE  (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND  (city=?3 or ?3 is null)  AND (state=?4 or ?4 is null) ", nativeQuery = true)
                      List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)





                      share|improve this answer


























                        0














                        Try this,



                        @Query(value = "SELECT * FROM zipcode WHERE  (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND  (city=?3 or ?3 is null)  AND (state=?4 or ?4 is null) ", nativeQuery = true)
                        List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)





                        share|improve this answer
























                          0












                          0








                          0






                          Try this,



                          @Query(value = "SELECT * FROM zipcode WHERE  (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND  (city=?3 or ?3 is null)  AND (state=?4 or ?4 is null) ", nativeQuery = true)
                          List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)





                          share|improve this answer












                          Try this,



                          @Query(value = "SELECT * FROM zipcode WHERE  (id=?1 or ?1 is null) AND (zip=?2 or ?2 is null) AND  (city=?3 or ?3 is null)  AND (state=?4 or ?4 is null) ", nativeQuery = true)
                          List<Zipcode> getZipCodes(Integer id, Integer zip, String city, String state)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered yesterday









                          Himesh goswami

                          653315




                          653315






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944169%2fget-all-the-records-of-column-in-spring-data-jpa-even-we-specify-it-as-paramete%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Mossoró

                              Error while reading .h5 file using the rhdf5 package in R

                              Pushsharp Apns notification error: 'InvalidToken'