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

          How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

          155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

          Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?