How to return a select query from jpa repository with foreign key type












1















I am trying to do this:



@Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);


But I get this error:



  org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]









share|improve this question





























    1















    I am trying to do this:



    @Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
    List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);


    But I get this error:



      org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]









    share|improve this question



























      1












      1








      1








      I am trying to do this:



      @Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
      List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);


      But I get this error:



        org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]









      share|improve this question
















      I am trying to do this:



      @Query(value = "SELECT DISTINCT c.* FROM comarca c INNER JOIN debito_negativacao d ON d.comarca_id = c.id WHERE d.status = :status", nativeQuery = true)
      List<Comarca> findDistinctComarcaByStatus(@Param("status") String status);


      But I get this error:



        org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object] to type [com.hc.projects.model.Comarca] for value '{9, 0, 7323}'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.math.BigInteger] to type [com.hc.projects.model.Comarca]






      java spring hibernate spring-data-jpa






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 18 at 18:54









      Billy Frost

      1,74598




      1,74598










      asked Jan 18 at 14:18









      Kaique DiasKaique Dias

      126




      126
























          4 Answers
          4






          active

          oldest

          votes


















          0














          If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.



              List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());


          ++






          share|improve this answer

































            0














            You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.



            As didn't provide the tables I can only guess:



            @Query(value = "SELECT DISTINCT * FROM comarca c " +
            "JOIN debito_negativacao d ON d.comarca_id = c.id "+
            "WHERE d.debito_negativacao.status= :status", nativeQuery = true)
            List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);





            share|improve this answer
























            • ok, i'll try to do this!

              – Kaique Dias
              Jan 18 at 14:25











            • i did as you say, but the error persists

              – Kaique Dias
              Jan 18 at 15:16



















            0














            Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
            because comarca_id is a biginteger I guess.
            If you want a Comarca list, you have to request on all your table.






            share|improve this answer
























            • i did as you say, but the error persists. Question att

              – Kaique Dias
              Jan 18 at 15:17



















            0














            If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:



            ------------------------------
            | comarca_id| key | status |
            | 1 | A | your_state|
            | 1 | B | your_state|
            | 2 | C | your_state|
            ------------------------------


            What would you get in this case?



            SELECT DISTINCT comarca_id FROM comarca; will return 1,2



            However, how can you merge two (or more) entries which have the same comarca_idand status?



            This leaves you with three cases:




            1. you assume comarca_id + status is unique -> you don't need the DISTINCT query

            2. There might be more than one row with same comarca_id and status -> you can't make the query distinct

            3. you only want the distinct comarca_id values -> make your method return List<BigInteger>






            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%2f54255858%2fhow-to-return-a-select-query-from-jpa-repository-with-foreign-key-type%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.



                  List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());


              ++






              share|improve this answer






























                0














                If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.



                    List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());


                ++






                share|improve this answer




























                  0












                  0








                  0







                  If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.



                      List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());


                  ++






                  share|improve this answer















                  If, in a second time, you want to isolate a list of comarca_id, try to stream your request result.



                      List<Comarca> comarca = debitoNegativacao.stream().map(dn -> dn.getComarca()).distinct().collect(Collectors.toList());


                  ++







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 18 at 22:43









                  Kaique Dias

                  126




                  126










                  answered Jan 18 at 14:34









                  LovegiverLovegiver

                  12711




                  12711

























                      0














                      You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.



                      As didn't provide the tables I can only guess:



                      @Query(value = "SELECT DISTINCT * FROM comarca c " +
                      "JOIN debito_negativacao d ON d.comarca_id = c.id "+
                      "WHERE d.debito_negativacao.status= :status", nativeQuery = true)
                      List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);





                      share|improve this answer
























                      • ok, i'll try to do this!

                        – Kaique Dias
                        Jan 18 at 14:25











                      • i did as you say, but the error persists

                        – Kaique Dias
                        Jan 18 at 15:16
















                      0














                      You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.



                      As didn't provide the tables I can only guess:



                      @Query(value = "SELECT DISTINCT * FROM comarca c " +
                      "JOIN debito_negativacao d ON d.comarca_id = c.id "+
                      "WHERE d.debito_negativacao.status= :status", nativeQuery = true)
                      List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);





                      share|improve this answer
























                      • ok, i'll try to do this!

                        – Kaique Dias
                        Jan 18 at 14:25











                      • i did as you say, but the error persists

                        – Kaique Dias
                        Jan 18 at 15:16














                      0












                      0








                      0







                      You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.



                      As didn't provide the tables I can only guess:



                      @Query(value = "SELECT DISTINCT * FROM comarca c " +
                      "JOIN debito_negativacao d ON d.comarca_id = c.id "+
                      "WHERE d.debito_negativacao.status= :status", nativeQuery = true)
                      List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);





                      share|improve this answer













                      You have to return all columns that are necessary to construct a Comarca. So you will have to join the table.



                      As didn't provide the tables I can only guess:



                      @Query(value = "SELECT DISTINCT * FROM comarca c " +
                      "JOIN debito_negativacao d ON d.comarca_id = c.id "+
                      "WHERE d.debito_negativacao.status= :status", nativeQuery = true)
                      List<Comarca> findDistinctComarcaIdByStatus(@Param("status") String status);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 18 at 14:24









                      Simon MartinelliSimon Martinelli

                      5,87111230




                      5,87111230













                      • ok, i'll try to do this!

                        – Kaique Dias
                        Jan 18 at 14:25











                      • i did as you say, but the error persists

                        – Kaique Dias
                        Jan 18 at 15:16



















                      • ok, i'll try to do this!

                        – Kaique Dias
                        Jan 18 at 14:25











                      • i did as you say, but the error persists

                        – Kaique Dias
                        Jan 18 at 15:16

















                      ok, i'll try to do this!

                      – Kaique Dias
                      Jan 18 at 14:25





                      ok, i'll try to do this!

                      – Kaique Dias
                      Jan 18 at 14:25













                      i did as you say, but the error persists

                      – Kaique Dias
                      Jan 18 at 15:16





                      i did as you say, but the error persists

                      – Kaique Dias
                      Jan 18 at 15:16











                      0














                      Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
                      because comarca_id is a biginteger I guess.
                      If you want a Comarca list, you have to request on all your table.






                      share|improve this answer
























                      • i did as you say, but the error persists. Question att

                        – Kaique Dias
                        Jan 18 at 15:17
















                      0














                      Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
                      because comarca_id is a biginteger I guess.
                      If you want a Comarca list, you have to request on all your table.






                      share|improve this answer
























                      • i did as you say, but the error persists. Question att

                        – Kaique Dias
                        Jan 18 at 15:17














                      0












                      0








                      0







                      Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
                      because comarca_id is a biginteger I guess.
                      If you want a Comarca list, you have to request on all your table.






                      share|improve this answer













                      Your request tells that you want a list of BigInteger : SELECT DISTINCT comarca_id...
                      because comarca_id is a biginteger I guess.
                      If you want a Comarca list, you have to request on all your table.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 18 at 14:31









                      LovegiverLovegiver

                      12711




                      12711













                      • i did as you say, but the error persists. Question att

                        – Kaique Dias
                        Jan 18 at 15:17



















                      • i did as you say, but the error persists. Question att

                        – Kaique Dias
                        Jan 18 at 15:17

















                      i did as you say, but the error persists. Question att

                      – Kaique Dias
                      Jan 18 at 15:17





                      i did as you say, but the error persists. Question att

                      – Kaique Dias
                      Jan 18 at 15:17











                      0














                      If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:



                      ------------------------------
                      | comarca_id| key | status |
                      | 1 | A | your_state|
                      | 1 | B | your_state|
                      | 2 | C | your_state|
                      ------------------------------


                      What would you get in this case?



                      SELECT DISTINCT comarca_id FROM comarca; will return 1,2



                      However, how can you merge two (or more) entries which have the same comarca_idand status?



                      This leaves you with three cases:




                      1. you assume comarca_id + status is unique -> you don't need the DISTINCT query

                      2. There might be more than one row with same comarca_id and status -> you can't make the query distinct

                      3. you only want the distinct comarca_id values -> make your method return List<BigInteger>






                      share|improve this answer




























                        0














                        If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:



                        ------------------------------
                        | comarca_id| key | status |
                        | 1 | A | your_state|
                        | 1 | B | your_state|
                        | 2 | C | your_state|
                        ------------------------------


                        What would you get in this case?



                        SELECT DISTINCT comarca_id FROM comarca; will return 1,2



                        However, how can you merge two (or more) entries which have the same comarca_idand status?



                        This leaves you with three cases:




                        1. you assume comarca_id + status is unique -> you don't need the DISTINCT query

                        2. There might be more than one row with same comarca_id and status -> you can't make the query distinct

                        3. you only want the distinct comarca_id values -> make your method return List<BigInteger>






                        share|improve this answer


























                          0












                          0








                          0







                          If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:



                          ------------------------------
                          | comarca_id| key | status |
                          | 1 | A | your_state|
                          | 1 | B | your_state|
                          | 2 | C | your_state|
                          ------------------------------


                          What would you get in this case?



                          SELECT DISTINCT comarca_id FROM comarca; will return 1,2



                          However, how can you merge two (or more) entries which have the same comarca_idand status?



                          This leaves you with three cases:




                          1. you assume comarca_id + status is unique -> you don't need the DISTINCT query

                          2. There might be more than one row with same comarca_id and status -> you can't make the query distinct

                          3. you only want the distinct comarca_id values -> make your method return List<BigInteger>






                          share|improve this answer













                          If you want to use a distinct query that will return other columns than the distinct one you need some kind of strategy how to "merge" the objects. Imagine rows like this:



                          ------------------------------
                          | comarca_id| key | status |
                          | 1 | A | your_state|
                          | 1 | B | your_state|
                          | 2 | C | your_state|
                          ------------------------------


                          What would you get in this case?



                          SELECT DISTINCT comarca_id FROM comarca; will return 1,2



                          However, how can you merge two (or more) entries which have the same comarca_idand status?



                          This leaves you with three cases:




                          1. you assume comarca_id + status is unique -> you don't need the DISTINCT query

                          2. There might be more than one row with same comarca_id and status -> you can't make the query distinct

                          3. you only want the distinct comarca_id values -> make your method return List<BigInteger>







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 18 at 14:31









                          Abaddon666Abaddon666

                          782722




                          782722






























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255858%2fhow-to-return-a-select-query-from-jpa-repository-with-foreign-key-type%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

                              Homophylophilia

                              Updating UILabel text programmatically using a function

                              Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage