Why queryForObject Kotlin extension func returns nullable T? if it actually will throw EmptyResultDataAccessException?How to pass user define table type of sql server as a parameter to a store procedure from javaHow exactly work the Spring RowMapper interface?Static extension methods in KotlinKotlin mutableMap.put returns nullableJdbctemplate queryforObject: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0Kotlin methods with Vararg as First Parameterinvoke() on out-projected Function objectWhy intent.getParcelableExtra don't return nullable type in kotlin?Kotlin - nullable receiver extension won't accept non-nullable equivalentKotlin does not check for return statement in function with some return type in function signature

If two black hole event horizons overlap (touch) can they ever separate again?

pgfmath does not work

How did they film the Invisible Man being invisible, in 1933?

Why isn't UDP with reliability (implemented at Application layer) a substitute of TCP?

What verb for taking advantage fits in "I don't want to ________ on the friendship"?

Robots in a spaceship

Transactional Email API: TriggeredSend definition not enabled for this route

if a USA citizen marries a foreign citizen who has kid from previous marriage

Can a successful book series let the bad guy win?

How can I deal with extreme temperatures in a hotel room?

How do I ask a good question about a topic I am not completely familiar with?

Why would anyone even use a Portkey?

Why did the Apple //e make a hideous noise if you inserted the disk upside down?

Do home values typically rise and fall consistently across different price ranges?

Cooking a nice pan seared steak for picky eaters

Is it okay to fade a human face just to create some space to place important content over it?

How did Lefschetz do mathematics without hands?

Story where diplomats use codes for emotions

Adjective for 'made of pus' or 'corrupted by pus' or something of something of pus

A quine of sorts

Bin Packing with Relational Penalization

On what to compliment someone with anorexia in order to improve their body image?

"I am [the / an] owner of a bookstore"?

Transferring Data From One Table to Another Using Multiple Keys in ArcPy?



Why queryForObject Kotlin extension func returns nullable T? if it actually will throw EmptyResultDataAccessException?


How to pass user define table type of sql server as a parameter to a store procedure from javaHow exactly work the Spring RowMapper interface?Static extension methods in KotlinKotlin mutableMap.put returns nullableJdbctemplate queryforObject: EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0Kotlin methods with Vararg as First Parameterinvoke() on out-projected Function objectWhy intent.getParcelableExtra don't return nullable type in kotlin?Kotlin - nullable receiver extension won't accept non-nullable equivalentKotlin does not check for return statement in function with some return type in function signature






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Using Kotlin and Spring 5 for some simple project.
I would like to get single record from database by id using queryForObject.
My query is a 'simple select by id':



jdbc.queryForObject("select id, name from example where id = ?", id)
rs: ResultSet, _: Int -> NamedEnt(rs.getLong("id"), rs.getString("name")


In JdbcOperationsExtensions.kt it is declared to return nullable type T?:



fun <T : Any> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? =
queryForObject(sql, RowMapper resultSet, i -> function(resultSet, i) , *args)


In practice when I pass not existing identifier, I face with:




org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0




Then I do not understand what is the point of returning nullable type? You either receive a single record or exception. Or I miss something?










share|improve this question




























    0















    Using Kotlin and Spring 5 for some simple project.
    I would like to get single record from database by id using queryForObject.
    My query is a 'simple select by id':



    jdbc.queryForObject("select id, name from example where id = ?", id)
    rs: ResultSet, _: Int -> NamedEnt(rs.getLong("id"), rs.getString("name")


    In JdbcOperationsExtensions.kt it is declared to return nullable type T?:



    fun <T : Any> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? =
    queryForObject(sql, RowMapper resultSet, i -> function(resultSet, i) , *args)


    In practice when I pass not existing identifier, I face with:




    org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0




    Then I do not understand what is the point of returning nullable type? You either receive a single record or exception. Or I miss something?










    share|improve this question
























      0












      0








      0








      Using Kotlin and Spring 5 for some simple project.
      I would like to get single record from database by id using queryForObject.
      My query is a 'simple select by id':



      jdbc.queryForObject("select id, name from example where id = ?", id)
      rs: ResultSet, _: Int -> NamedEnt(rs.getLong("id"), rs.getString("name")


      In JdbcOperationsExtensions.kt it is declared to return nullable type T?:



      fun <T : Any> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? =
      queryForObject(sql, RowMapper resultSet, i -> function(resultSet, i) , *args)


      In practice when I pass not existing identifier, I face with:




      org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0




      Then I do not understand what is the point of returning nullable type? You either receive a single record or exception. Or I miss something?










      share|improve this question














      Using Kotlin and Spring 5 for some simple project.
      I would like to get single record from database by id using queryForObject.
      My query is a 'simple select by id':



      jdbc.queryForObject("select id, name from example where id = ?", id)
      rs: ResultSet, _: Int -> NamedEnt(rs.getLong("id"), rs.getString("name")


      In JdbcOperationsExtensions.kt it is declared to return nullable type T?:



      fun <T : Any> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? =
      queryForObject(sql, RowMapper resultSet, i -> function(resultSet, i) , *args)


      In practice when I pass not existing identifier, I face with:




      org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0




      Then I do not understand what is the point of returning nullable type? You either receive a single record or exception. Or I miss something?







      kotlin spring-jdbc spring-kotlin






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 14:54









      DerpDerp

      1,4111 gold badge17 silver badges32 bronze badges




      1,4111 gold badge17 silver badges32 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          1














          JdbcOperationsExtensions.kt adds some extension functions to the org.springframework.jdbc.core.JdbcOperations interface (written in Java). If you look at the JavaDocs for queryForObject in that, it says:



          @return the single mapped object (may be @code null if the given
          @link RowMapper returned @code null)


          See here for full source code of the JdbcOperations Java class.



          So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned, hence the nullable type.



          Except... as pointed out by @AlexeyRomanov, this particular overload of queryForObject takes in a lambda which returns T, so can't ever return null, so arguably this overload could return T, not T?. Maybe it's a bit inconsistent that this lambda in Kotlin can't return null, but the JavaDocs on the very similar overload in the Java class explicitly state that it (RowMapper) should be allowed to return null.



          Regardless of that point, some other overloads of queryForObject simply call to down to the Java-written overload, and because it's written in Java, it's possible that it could return a null. So for them it does seem to make sense for it to be a nullable return value. In which case arguably it's a nice bit of consistency that all the overloads do in fact return the nullable T.






          share|improve this answer

























          • "So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned" Except they don't: function: (ResultSet, Int) -> T can't return null, so the RowMapper using it can't return null.

            – Alexey Romanov
            Mar 25 at 17:26











          • Ha! Well spotted @AlexeyRomanov: I missed that. I wonder if this is a small bug in that extension method then: maybe the lambda passed into queryForObject should be declared as having a return type of T?. Because the JavaDocs seem to suggest that the RowMapper should be allowed to return null.

            – Yoni Gibbs
            Mar 25 at 19:41











          • I'd say so, yes. It should probably be inline too. Might be worth reporting...

            – Alexey Romanov
            Mar 25 at 21:28











          • Agreed. Issue logged here.

            – Yoni Gibbs
            Mar 26 at 10:27











          • @AlexeyRomanov Could you elaborate on why these extensions should be inline? Performance?

            – Sébastien Deleuze
            Mar 26 at 13:42










          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%2f55340598%2fwhy-queryforobject-kotlin-extension-func-returns-nullable-t-if-it-actually-will%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









          1














          JdbcOperationsExtensions.kt adds some extension functions to the org.springframework.jdbc.core.JdbcOperations interface (written in Java). If you look at the JavaDocs for queryForObject in that, it says:



          @return the single mapped object (may be @code null if the given
          @link RowMapper returned @code null)


          See here for full source code of the JdbcOperations Java class.



          So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned, hence the nullable type.



          Except... as pointed out by @AlexeyRomanov, this particular overload of queryForObject takes in a lambda which returns T, so can't ever return null, so arguably this overload could return T, not T?. Maybe it's a bit inconsistent that this lambda in Kotlin can't return null, but the JavaDocs on the very similar overload in the Java class explicitly state that it (RowMapper) should be allowed to return null.



          Regardless of that point, some other overloads of queryForObject simply call to down to the Java-written overload, and because it's written in Java, it's possible that it could return a null. So for them it does seem to make sense for it to be a nullable return value. In which case arguably it's a nice bit of consistency that all the overloads do in fact return the nullable T.






          share|improve this answer

























          • "So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned" Except they don't: function: (ResultSet, Int) -> T can't return null, so the RowMapper using it can't return null.

            – Alexey Romanov
            Mar 25 at 17:26











          • Ha! Well spotted @AlexeyRomanov: I missed that. I wonder if this is a small bug in that extension method then: maybe the lambda passed into queryForObject should be declared as having a return type of T?. Because the JavaDocs seem to suggest that the RowMapper should be allowed to return null.

            – Yoni Gibbs
            Mar 25 at 19:41











          • I'd say so, yes. It should probably be inline too. Might be worth reporting...

            – Alexey Romanov
            Mar 25 at 21:28











          • Agreed. Issue logged here.

            – Yoni Gibbs
            Mar 26 at 10:27











          • @AlexeyRomanov Could you elaborate on why these extensions should be inline? Performance?

            – Sébastien Deleuze
            Mar 26 at 13:42















          1














          JdbcOperationsExtensions.kt adds some extension functions to the org.springframework.jdbc.core.JdbcOperations interface (written in Java). If you look at the JavaDocs for queryForObject in that, it says:



          @return the single mapped object (may be @code null if the given
          @link RowMapper returned @code null)


          See here for full source code of the JdbcOperations Java class.



          So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned, hence the nullable type.



          Except... as pointed out by @AlexeyRomanov, this particular overload of queryForObject takes in a lambda which returns T, so can't ever return null, so arguably this overload could return T, not T?. Maybe it's a bit inconsistent that this lambda in Kotlin can't return null, but the JavaDocs on the very similar overload in the Java class explicitly state that it (RowMapper) should be allowed to return null.



          Regardless of that point, some other overloads of queryForObject simply call to down to the Java-written overload, and because it's written in Java, it's possible that it could return a null. So for them it does seem to make sense for it to be a nullable return value. In which case arguably it's a nice bit of consistency that all the overloads do in fact return the nullable T.






          share|improve this answer

























          • "So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned" Except they don't: function: (ResultSet, Int) -> T can't return null, so the RowMapper using it can't return null.

            – Alexey Romanov
            Mar 25 at 17:26











          • Ha! Well spotted @AlexeyRomanov: I missed that. I wonder if this is a small bug in that extension method then: maybe the lambda passed into queryForObject should be declared as having a return type of T?. Because the JavaDocs seem to suggest that the RowMapper should be allowed to return null.

            – Yoni Gibbs
            Mar 25 at 19:41











          • I'd say so, yes. It should probably be inline too. Might be worth reporting...

            – Alexey Romanov
            Mar 25 at 21:28











          • Agreed. Issue logged here.

            – Yoni Gibbs
            Mar 26 at 10:27











          • @AlexeyRomanov Could you elaborate on why these extensions should be inline? Performance?

            – Sébastien Deleuze
            Mar 26 at 13:42













          1












          1








          1







          JdbcOperationsExtensions.kt adds some extension functions to the org.springframework.jdbc.core.JdbcOperations interface (written in Java). If you look at the JavaDocs for queryForObject in that, it says:



          @return the single mapped object (may be @code null if the given
          @link RowMapper returned @code null)


          See here for full source code of the JdbcOperations Java class.



          So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned, hence the nullable type.



          Except... as pointed out by @AlexeyRomanov, this particular overload of queryForObject takes in a lambda which returns T, so can't ever return null, so arguably this overload could return T, not T?. Maybe it's a bit inconsistent that this lambda in Kotlin can't return null, but the JavaDocs on the very similar overload in the Java class explicitly state that it (RowMapper) should be allowed to return null.



          Regardless of that point, some other overloads of queryForObject simply call to down to the Java-written overload, and because it's written in Java, it's possible that it could return a null. So for them it does seem to make sense for it to be a nullable return value. In which case arguably it's a nice bit of consistency that all the overloads do in fact return the nullable T.






          share|improve this answer















          JdbcOperationsExtensions.kt adds some extension functions to the org.springframework.jdbc.core.JdbcOperations interface (written in Java). If you look at the JavaDocs for queryForObject in that, it says:



          @return the single mapped object (may be @code null if the given
          @link RowMapper returned @code null)


          See here for full source code of the JdbcOperations Java class.



          So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned, hence the nullable type.



          Except... as pointed out by @AlexeyRomanov, this particular overload of queryForObject takes in a lambda which returns T, so can't ever return null, so arguably this overload could return T, not T?. Maybe it's a bit inconsistent that this lambda in Kotlin can't return null, but the JavaDocs on the very similar overload in the Java class explicitly state that it (RowMapper) should be allowed to return null.



          Regardless of that point, some other overloads of queryForObject simply call to down to the Java-written overload, and because it's written in Java, it's possible that it could return a null. So for them it does seem to make sense for it to be a nullable return value. In which case arguably it's a nice bit of consistency that all the overloads do in fact return the nullable T.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 19:51

























          answered Mar 25 at 15:20









          Yoni GibbsYoni Gibbs

          1,9791 silver badge18 bronze badges




          1,9791 silver badge18 bronze badges












          • "So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned" Except they don't: function: (ResultSet, Int) -> T can't return null, so the RowMapper using it can't return null.

            – Alexey Romanov
            Mar 25 at 17:26











          • Ha! Well spotted @AlexeyRomanov: I missed that. I wonder if this is a small bug in that extension method then: maybe the lambda passed into queryForObject should be declared as having a return type of T?. Because the JavaDocs seem to suggest that the RowMapper should be allowed to return null.

            – Yoni Gibbs
            Mar 25 at 19:41











          • I'd say so, yes. It should probably be inline too. Might be worth reporting...

            – Alexey Romanov
            Mar 25 at 21:28











          • Agreed. Issue logged here.

            – Yoni Gibbs
            Mar 26 at 10:27











          • @AlexeyRomanov Could you elaborate on why these extensions should be inline? Performance?

            – Sébastien Deleuze
            Mar 26 at 13:42

















          • "So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned" Except they don't: function: (ResultSet, Int) -> T can't return null, so the RowMapper using it can't return null.

            – Alexey Romanov
            Mar 25 at 17:26











          • Ha! Well spotted @AlexeyRomanov: I missed that. I wonder if this is a small bug in that extension method then: maybe the lambda passed into queryForObject should be declared as having a return type of T?. Because the JavaDocs seem to suggest that the RowMapper should be allowed to return null.

            – Yoni Gibbs
            Mar 25 at 19:41











          • I'd say so, yes. It should probably be inline too. Might be worth reporting...

            – Alexey Romanov
            Mar 25 at 21:28











          • Agreed. Issue logged here.

            – Yoni Gibbs
            Mar 26 at 10:27











          • @AlexeyRomanov Could you elaborate on why these extensions should be inline? Performance?

            – Sébastien Deleuze
            Mar 26 at 13:42
















          "So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned" Except they don't: function: (ResultSet, Int) -> T can't return null, so the RowMapper using it can't return null.

          – Alexey Romanov
          Mar 25 at 17:26





          "So the Kotlin-written extension functions need to adhere to this and allow nulls to be returned" Except they don't: function: (ResultSet, Int) -> T can't return null, so the RowMapper using it can't return null.

          – Alexey Romanov
          Mar 25 at 17:26













          Ha! Well spotted @AlexeyRomanov: I missed that. I wonder if this is a small bug in that extension method then: maybe the lambda passed into queryForObject should be declared as having a return type of T?. Because the JavaDocs seem to suggest that the RowMapper should be allowed to return null.

          – Yoni Gibbs
          Mar 25 at 19:41





          Ha! Well spotted @AlexeyRomanov: I missed that. I wonder if this is a small bug in that extension method then: maybe the lambda passed into queryForObject should be declared as having a return type of T?. Because the JavaDocs seem to suggest that the RowMapper should be allowed to return null.

          – Yoni Gibbs
          Mar 25 at 19:41













          I'd say so, yes. It should probably be inline too. Might be worth reporting...

          – Alexey Romanov
          Mar 25 at 21:28





          I'd say so, yes. It should probably be inline too. Might be worth reporting...

          – Alexey Romanov
          Mar 25 at 21:28













          Agreed. Issue logged here.

          – Yoni Gibbs
          Mar 26 at 10:27





          Agreed. Issue logged here.

          – Yoni Gibbs
          Mar 26 at 10:27













          @AlexeyRomanov Could you elaborate on why these extensions should be inline? Performance?

          – Sébastien Deleuze
          Mar 26 at 13:42





          @AlexeyRomanov Could you elaborate on why these extensions should be inline? Performance?

          – Sébastien Deleuze
          Mar 26 at 13:42








          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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%2f55340598%2fwhy-queryforobject-kotlin-extension-func-returns-nullable-t-if-it-actually-will%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현