findAllUseCountTop1ByIsActiveOrderById(Boolean isActive) does not generate a query with “LIMIT 1”

Multi tool use
Multi tool use












-2















Why does JPA not apply "LIMIT 1" to the query?
I am trying to write a repository function in SpringBoot application to get the first result from the top where "isActive" is TRUE when ordered by ID. This function generates a query that does not include "LIMIT 1" this returns multiple results and is failing with "query did not return a unique result:4" exception. I am using Hibernate Core v5.3.7.Final with MySQL v8.0.12 as the database.



Generated Query :



select phoneinfo0_.id as id1_0_, phoneinfo0_.Phone as Phone2_0_, phoneinfo0_.isActive as isActive3_0_, phoneinfo0_.useCount as useCount4_0_ from contactNumbers phoneinfo0_ where phoneinfo0_.isActive=1 order by phoneinfo0_.id asc;


Result Returned :



+--------+------------+--------------+--------------+
| id1_0_ | Phone2_0_ | isActive3_0_ | useCount4_0_ |
+--------+------------+--------------+--------------+
| 1 | 1111111111 | 1 | 0 |
| 2 | 9999999999 | 1 | 0 |
| 3 | 9000000000 | 1 | 0 |
| 4 | 2222222222 | 1 | 0 |
+--------+------------+--------------+--------------+


Also please tell me if "ORDER BY ID" is necessary or is it done by default on every select if ID is Primary key auto-incremented?










share|improve this question





























    -2















    Why does JPA not apply "LIMIT 1" to the query?
    I am trying to write a repository function in SpringBoot application to get the first result from the top where "isActive" is TRUE when ordered by ID. This function generates a query that does not include "LIMIT 1" this returns multiple results and is failing with "query did not return a unique result:4" exception. I am using Hibernate Core v5.3.7.Final with MySQL v8.0.12 as the database.



    Generated Query :



    select phoneinfo0_.id as id1_0_, phoneinfo0_.Phone as Phone2_0_, phoneinfo0_.isActive as isActive3_0_, phoneinfo0_.useCount as useCount4_0_ from contactNumbers phoneinfo0_ where phoneinfo0_.isActive=1 order by phoneinfo0_.id asc;


    Result Returned :



    +--------+------------+--------------+--------------+
    | id1_0_ | Phone2_0_ | isActive3_0_ | useCount4_0_ |
    +--------+------------+--------------+--------------+
    | 1 | 1111111111 | 1 | 0 |
    | 2 | 9999999999 | 1 | 0 |
    | 3 | 9000000000 | 1 | 0 |
    | 4 | 2222222222 | 1 | 0 |
    +--------+------------+--------------+--------------+


    Also please tell me if "ORDER BY ID" is necessary or is it done by default on every select if ID is Primary key auto-incremented?










    share|improve this question



























      -2












      -2








      -2








      Why does JPA not apply "LIMIT 1" to the query?
      I am trying to write a repository function in SpringBoot application to get the first result from the top where "isActive" is TRUE when ordered by ID. This function generates a query that does not include "LIMIT 1" this returns multiple results and is failing with "query did not return a unique result:4" exception. I am using Hibernate Core v5.3.7.Final with MySQL v8.0.12 as the database.



      Generated Query :



      select phoneinfo0_.id as id1_0_, phoneinfo0_.Phone as Phone2_0_, phoneinfo0_.isActive as isActive3_0_, phoneinfo0_.useCount as useCount4_0_ from contactNumbers phoneinfo0_ where phoneinfo0_.isActive=1 order by phoneinfo0_.id asc;


      Result Returned :



      +--------+------------+--------------+--------------+
      | id1_0_ | Phone2_0_ | isActive3_0_ | useCount4_0_ |
      +--------+------------+--------------+--------------+
      | 1 | 1111111111 | 1 | 0 |
      | 2 | 9999999999 | 1 | 0 |
      | 3 | 9000000000 | 1 | 0 |
      | 4 | 2222222222 | 1 | 0 |
      +--------+------------+--------------+--------------+


      Also please tell me if "ORDER BY ID" is necessary or is it done by default on every select if ID is Primary key auto-incremented?










      share|improve this question
















      Why does JPA not apply "LIMIT 1" to the query?
      I am trying to write a repository function in SpringBoot application to get the first result from the top where "isActive" is TRUE when ordered by ID. This function generates a query that does not include "LIMIT 1" this returns multiple results and is failing with "query did not return a unique result:4" exception. I am using Hibernate Core v5.3.7.Final with MySQL v8.0.12 as the database.



      Generated Query :



      select phoneinfo0_.id as id1_0_, phoneinfo0_.Phone as Phone2_0_, phoneinfo0_.isActive as isActive3_0_, phoneinfo0_.useCount as useCount4_0_ from contactNumbers phoneinfo0_ where phoneinfo0_.isActive=1 order by phoneinfo0_.id asc;


      Result Returned :



      +--------+------------+--------------+--------------+
      | id1_0_ | Phone2_0_ | isActive3_0_ | useCount4_0_ |
      +--------+------------+--------------+--------------+
      | 1 | 1111111111 | 1 | 0 |
      | 2 | 9999999999 | 1 | 0 |
      | 3 | 9000000000 | 1 | 0 |
      | 4 | 2222222222 | 1 | 0 |
      +--------+------------+--------------+--------------+


      Also please tell me if "ORDER BY ID" is necessary or is it done by default on every select if ID is Primary key auto-incremented?







      mysql hibernate jpa spring-data-jpa






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 30 '18 at 23:49







      Mahesh P.

















      asked Dec 30 '18 at 23:42









      Mahesh P.Mahesh P.

      93




      93
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Try to rename it to one of this:



          findTop1UseCountByIsActiveOrderById(Boolean isActive)

          findFirstUseCountByIsActiveOrderById(Boolean isActive)





          share|improve this answer
























          • Hi @KonstantinLabun thank you for such a quick response. I have tried it and it works when I remove UseCount from the function name. The JPA straight on ignores column that I want to select and selects whole table instead. I got what I need though so thank you. Just one thing do you know how to select just one particular column or why is JPA selecting whole table instead of just getting "UseCount"?

            – Mahesh P.
            Dec 31 '18 at 1:00











          • Methods names is quite straightforward thing. Not sure if it will allow you to solve your problem. Try to use JPQL for it:

            – Konstantin Labun
            Dec 31 '18 at 1:06











          • I have just read about it. We cannot apparently. We have to use @Query when we want to select particular columns. My solution was "findFirstByIsActiveTrueOrderById()" as I always wanted only the active ones and getting whole object now as its other values could be used later. This helped too [docs.spring.io/spring-data/jpa/docs/current/reference/html/….

            – Mahesh P.
            Dec 31 '18 at 1:20













          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%2f53982346%2ffindallusecounttop1byisactiveorderbyidboolean-isactive-does-not-generate-a-que%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try to rename it to one of this:



          findTop1UseCountByIsActiveOrderById(Boolean isActive)

          findFirstUseCountByIsActiveOrderById(Boolean isActive)





          share|improve this answer
























          • Hi @KonstantinLabun thank you for such a quick response. I have tried it and it works when I remove UseCount from the function name. The JPA straight on ignores column that I want to select and selects whole table instead. I got what I need though so thank you. Just one thing do you know how to select just one particular column or why is JPA selecting whole table instead of just getting "UseCount"?

            – Mahesh P.
            Dec 31 '18 at 1:00











          • Methods names is quite straightforward thing. Not sure if it will allow you to solve your problem. Try to use JPQL for it:

            – Konstantin Labun
            Dec 31 '18 at 1:06











          • I have just read about it. We cannot apparently. We have to use @Query when we want to select particular columns. My solution was "findFirstByIsActiveTrueOrderById()" as I always wanted only the active ones and getting whole object now as its other values could be used later. This helped too [docs.spring.io/spring-data/jpa/docs/current/reference/html/….

            – Mahesh P.
            Dec 31 '18 at 1:20


















          0














          Try to rename it to one of this:



          findTop1UseCountByIsActiveOrderById(Boolean isActive)

          findFirstUseCountByIsActiveOrderById(Boolean isActive)





          share|improve this answer
























          • Hi @KonstantinLabun thank you for such a quick response. I have tried it and it works when I remove UseCount from the function name. The JPA straight on ignores column that I want to select and selects whole table instead. I got what I need though so thank you. Just one thing do you know how to select just one particular column or why is JPA selecting whole table instead of just getting "UseCount"?

            – Mahesh P.
            Dec 31 '18 at 1:00











          • Methods names is quite straightforward thing. Not sure if it will allow you to solve your problem. Try to use JPQL for it:

            – Konstantin Labun
            Dec 31 '18 at 1:06











          • I have just read about it. We cannot apparently. We have to use @Query when we want to select particular columns. My solution was "findFirstByIsActiveTrueOrderById()" as I always wanted only the active ones and getting whole object now as its other values could be used later. This helped too [docs.spring.io/spring-data/jpa/docs/current/reference/html/….

            – Mahesh P.
            Dec 31 '18 at 1:20
















          0












          0








          0







          Try to rename it to one of this:



          findTop1UseCountByIsActiveOrderById(Boolean isActive)

          findFirstUseCountByIsActiveOrderById(Boolean isActive)





          share|improve this answer













          Try to rename it to one of this:



          findTop1UseCountByIsActiveOrderById(Boolean isActive)

          findFirstUseCountByIsActiveOrderById(Boolean isActive)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 30 '18 at 23:52









          Konstantin LabunKonstantin Labun

          1,7231119




          1,7231119













          • Hi @KonstantinLabun thank you for such a quick response. I have tried it and it works when I remove UseCount from the function name. The JPA straight on ignores column that I want to select and selects whole table instead. I got what I need though so thank you. Just one thing do you know how to select just one particular column or why is JPA selecting whole table instead of just getting "UseCount"?

            – Mahesh P.
            Dec 31 '18 at 1:00











          • Methods names is quite straightforward thing. Not sure if it will allow you to solve your problem. Try to use JPQL for it:

            – Konstantin Labun
            Dec 31 '18 at 1:06











          • I have just read about it. We cannot apparently. We have to use @Query when we want to select particular columns. My solution was "findFirstByIsActiveTrueOrderById()" as I always wanted only the active ones and getting whole object now as its other values could be used later. This helped too [docs.spring.io/spring-data/jpa/docs/current/reference/html/….

            – Mahesh P.
            Dec 31 '18 at 1:20





















          • Hi @KonstantinLabun thank you for such a quick response. I have tried it and it works when I remove UseCount from the function name. The JPA straight on ignores column that I want to select and selects whole table instead. I got what I need though so thank you. Just one thing do you know how to select just one particular column or why is JPA selecting whole table instead of just getting "UseCount"?

            – Mahesh P.
            Dec 31 '18 at 1:00











          • Methods names is quite straightforward thing. Not sure if it will allow you to solve your problem. Try to use JPQL for it:

            – Konstantin Labun
            Dec 31 '18 at 1:06











          • I have just read about it. We cannot apparently. We have to use @Query when we want to select particular columns. My solution was "findFirstByIsActiveTrueOrderById()" as I always wanted only the active ones and getting whole object now as its other values could be used later. This helped too [docs.spring.io/spring-data/jpa/docs/current/reference/html/….

            – Mahesh P.
            Dec 31 '18 at 1:20



















          Hi @KonstantinLabun thank you for such a quick response. I have tried it and it works when I remove UseCount from the function name. The JPA straight on ignores column that I want to select and selects whole table instead. I got what I need though so thank you. Just one thing do you know how to select just one particular column or why is JPA selecting whole table instead of just getting "UseCount"?

          – Mahesh P.
          Dec 31 '18 at 1:00





          Hi @KonstantinLabun thank you for such a quick response. I have tried it and it works when I remove UseCount from the function name. The JPA straight on ignores column that I want to select and selects whole table instead. I got what I need though so thank you. Just one thing do you know how to select just one particular column or why is JPA selecting whole table instead of just getting "UseCount"?

          – Mahesh P.
          Dec 31 '18 at 1:00













          Methods names is quite straightforward thing. Not sure if it will allow you to solve your problem. Try to use JPQL for it:

          – Konstantin Labun
          Dec 31 '18 at 1:06





          Methods names is quite straightforward thing. Not sure if it will allow you to solve your problem. Try to use JPQL for it:

          – Konstantin Labun
          Dec 31 '18 at 1:06













          I have just read about it. We cannot apparently. We have to use @Query when we want to select particular columns. My solution was "findFirstByIsActiveTrueOrderById()" as I always wanted only the active ones and getting whole object now as its other values could be used later. This helped too [docs.spring.io/spring-data/jpa/docs/current/reference/html/….

          – Mahesh P.
          Dec 31 '18 at 1:20







          I have just read about it. We cannot apparently. We have to use @Query when we want to select particular columns. My solution was "findFirstByIsActiveTrueOrderById()" as I always wanted only the active ones and getting whole object now as its other values could be used later. This helped too [docs.spring.io/spring-data/jpa/docs/current/reference/html/….

          – Mahesh P.
          Dec 31 '18 at 1:20




















          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%2f53982346%2ffindallusecounttop1byisactiveorderbyidboolean-isactive-does-not-generate-a-que%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







          rweyRm7Cqns,E3as7Ikf
          ehOPLgv1,YswZwKVsEAknF g7lV1dIc

          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas