Native query in Spring Boot with WHERE condition for tuple in listSQL exception preparing query with ORMLiteHow to configure port for a Spring Boot applicationAny idea why H2 parser throws an exception at Oracle SQL statement “Syntax error in SQL statement ”SELECT SUBJECT_SEQ.NEXTVAL FROM[*] DUAL"?How to access meta-data tables in on a different schema to a Spring Boot + Batch application?Data Conversion Error H2 DatabaseH2 JDBC Prepared Statement ExceptionPersist LocalDate with Kotlin and SpringBootliquibase and spring boot integration test h2db Table “” not foundSpring JPA Repository is unable to create mapping tables of my entitiesTable not found on postresql connection from spring boot 2

Is it standard to have the first week's pay indefinitely withheld?

Why did nobody know who the Lord of this region was?

How does the Heat Metal spell interact with a follow-up Frostbite spell?

Can I pay my credit card?

How come Arya Stark didn't burn in Game of Thrones Season 8 Episode 5

Cannot remove door knob -- totally inaccessible!

How to handle professionally if colleagues has referred his relative and asking to take easy while taking interview

How to deal with the extreme reverberation in big cathedrals when playing the pipe organs?

Do we see some Unsullied doing this in S08E05?

Is Precocious Apprentice enough for Mystic Theurge?

How was the blinking terminal cursor invented?

How can I make dummy text (like lipsum) grey?

301 Redirects what does ([a-z]+)-(.*) and ([0-9]+)-(.*) mean

Is it standard for US-based universities to consider the ethnicity of an applicant during PhD admissions?

Would life always name the light from their sun "white"

Have there been any examples of re-usable rockets in the past?

Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?

What kind of environment would favor hermaphroditism in a sentient species over regular, old sexes?

Why is the A380’s with-reversers stopping distance the same as its no-reversers stopping distance?

Gimp perspective tool is not actually transforming

Promotion comes with unexpected 24/7/365 on-call

Why would you put your input amplifier in front of your filtering for and ECG signal?

Find the area of the rectangle

How can we delete item permanently without storing in Recycle Bin?



Native query in Spring Boot with WHERE condition for tuple in list


SQL exception preparing query with ORMLiteHow to configure port for a Spring Boot applicationAny idea why H2 parser throws an exception at Oracle SQL statement “Syntax error in SQL statement ”SELECT SUBJECT_SEQ.NEXTVAL FROM[*] DUAL"?How to access meta-data tables in on a different schema to a Spring Boot + Batch application?Data Conversion Error H2 DatabaseH2 JDBC Prepared Statement ExceptionPersist LocalDate with Kotlin and SpringBootliquibase and spring boot integration test h2db Table “” not foundSpring JPA Repository is unable to create mapping tables of my entitiesTable not found on postresql connection from spring boot 2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















When sending plain SQL to PostgreSQL like the following:



CREATE TABLE things (
id BIGINT PRIMARY KEY NOT NULL,
foo BIGINT NOT NULL,
bar BIGINT NOT NULL
);


INSERT INTO things VALUES (9900, 1, 2);
INSERT INTO things VALUES (9901, 3, 4);
INSERT INTO things VALUES (9902, 1, 4);
SELECT * FROM things WHERE foo IN (1, 2);
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


it works as expected.



However when trying to do the same thing using Spring Boot, it fails. Here is my minimal example (Kotlin):



The entity:



import org.springframework.data.jpa.domain.AbstractPersistable
import javax.persistence.Entity
import javax.persistence.Table

@Entity
@Table(name = "things")
class Thing(
val foo: Long,
val bar: Long
) : AbstractPersistable<Long>()


The repository:



import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface ThingRepository : JpaRepository<Thing, Long>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE foo IN (1, 2);")
fun selectNativeByFoo(): Iterable<Thing>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));")
fun selectNativeByFooAndBar(): Iterable<Thing>



A call to selectNativeByFoo works fine, but calling selectNativeByFooAndBar fails with the following exception:



org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement


Caused by: org.h2.jdbc.JdbcSQLException: Invalid parameter count for "VALUES", expected count: "1"; SQL statement:
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4)); [7001-197]


What am I doing wrong?










share|improve this question

















  • 1





    In your SQL experiment, you're using PostgreSQL. In your exception, the exception is a org.h2.jdbc.JdbcSQLException, showing that you're not using PostgreSQL, but H2. My guess is that your actual production database is PostgreSQL. I would thus run tests against PostgreSQL, not H2.

    – JB Nizet
    Mar 23 at 16:41











  • @JBNizet Thanks, you are absolutely right. I am using H2. However in my application.yaml I have spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL, and I (maybe too naively) assumed, this suffices to make it work in a compatible way.

    – Tobias Hermann
    Mar 23 at 17:03











  • Test it with the real thing. My guess is that indeed, you were too naive :-)

    – JB Nizet
    Mar 23 at 17:04


















0















When sending plain SQL to PostgreSQL like the following:



CREATE TABLE things (
id BIGINT PRIMARY KEY NOT NULL,
foo BIGINT NOT NULL,
bar BIGINT NOT NULL
);


INSERT INTO things VALUES (9900, 1, 2);
INSERT INTO things VALUES (9901, 3, 4);
INSERT INTO things VALUES (9902, 1, 4);
SELECT * FROM things WHERE foo IN (1, 2);
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


it works as expected.



However when trying to do the same thing using Spring Boot, it fails. Here is my minimal example (Kotlin):



The entity:



import org.springframework.data.jpa.domain.AbstractPersistable
import javax.persistence.Entity
import javax.persistence.Table

@Entity
@Table(name = "things")
class Thing(
val foo: Long,
val bar: Long
) : AbstractPersistable<Long>()


The repository:



import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface ThingRepository : JpaRepository<Thing, Long>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE foo IN (1, 2);")
fun selectNativeByFoo(): Iterable<Thing>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));")
fun selectNativeByFooAndBar(): Iterable<Thing>



A call to selectNativeByFoo works fine, but calling selectNativeByFooAndBar fails with the following exception:



org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement


Caused by: org.h2.jdbc.JdbcSQLException: Invalid parameter count for "VALUES", expected count: "1"; SQL statement:
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4)); [7001-197]


What am I doing wrong?










share|improve this question

















  • 1





    In your SQL experiment, you're using PostgreSQL. In your exception, the exception is a org.h2.jdbc.JdbcSQLException, showing that you're not using PostgreSQL, but H2. My guess is that your actual production database is PostgreSQL. I would thus run tests against PostgreSQL, not H2.

    – JB Nizet
    Mar 23 at 16:41











  • @JBNizet Thanks, you are absolutely right. I am using H2. However in my application.yaml I have spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL, and I (maybe too naively) assumed, this suffices to make it work in a compatible way.

    – Tobias Hermann
    Mar 23 at 17:03











  • Test it with the real thing. My guess is that indeed, you were too naive :-)

    – JB Nizet
    Mar 23 at 17:04














0












0








0








When sending plain SQL to PostgreSQL like the following:



CREATE TABLE things (
id BIGINT PRIMARY KEY NOT NULL,
foo BIGINT NOT NULL,
bar BIGINT NOT NULL
);


INSERT INTO things VALUES (9900, 1, 2);
INSERT INTO things VALUES (9901, 3, 4);
INSERT INTO things VALUES (9902, 1, 4);
SELECT * FROM things WHERE foo IN (1, 2);
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


it works as expected.



However when trying to do the same thing using Spring Boot, it fails. Here is my minimal example (Kotlin):



The entity:



import org.springframework.data.jpa.domain.AbstractPersistable
import javax.persistence.Entity
import javax.persistence.Table

@Entity
@Table(name = "things")
class Thing(
val foo: Long,
val bar: Long
) : AbstractPersistable<Long>()


The repository:



import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface ThingRepository : JpaRepository<Thing, Long>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE foo IN (1, 2);")
fun selectNativeByFoo(): Iterable<Thing>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));")
fun selectNativeByFooAndBar(): Iterable<Thing>



A call to selectNativeByFoo works fine, but calling selectNativeByFooAndBar fails with the following exception:



org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement


Caused by: org.h2.jdbc.JdbcSQLException: Invalid parameter count for "VALUES", expected count: "1"; SQL statement:
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4)); [7001-197]


What am I doing wrong?










share|improve this question














When sending plain SQL to PostgreSQL like the following:



CREATE TABLE things (
id BIGINT PRIMARY KEY NOT NULL,
foo BIGINT NOT NULL,
bar BIGINT NOT NULL
);


INSERT INTO things VALUES (9900, 1, 2);
INSERT INTO things VALUES (9901, 3, 4);
INSERT INTO things VALUES (9902, 1, 4);
SELECT * FROM things WHERE foo IN (1, 2);
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


it works as expected.



However when trying to do the same thing using Spring Boot, it fails. Here is my minimal example (Kotlin):



The entity:



import org.springframework.data.jpa.domain.AbstractPersistable
import javax.persistence.Entity
import javax.persistence.Table

@Entity
@Table(name = "things")
class Thing(
val foo: Long,
val bar: Long
) : AbstractPersistable<Long>()


The repository:



import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface ThingRepository : JpaRepository<Thing, Long>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE foo IN (1, 2);")
fun selectNativeByFoo(): Iterable<Thing>

@Query(nativeQuery = true, value = "SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));")
fun selectNativeByFooAndBar(): Iterable<Thing>



A call to selectNativeByFoo works fine, but calling selectNativeByFooAndBar fails with the following exception:



org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement


Caused by: org.h2.jdbc.JdbcSQLException: Invalid parameter count for "VALUES", expected count: "1"; SQL statement:
SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4)); [7001-197]


What am I doing wrong?







sql spring-boot jdbc kotlin spring-data-jpa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 16:16









Tobias HermannTobias Hermann

3,04022457




3,04022457







  • 1





    In your SQL experiment, you're using PostgreSQL. In your exception, the exception is a org.h2.jdbc.JdbcSQLException, showing that you're not using PostgreSQL, but H2. My guess is that your actual production database is PostgreSQL. I would thus run tests against PostgreSQL, not H2.

    – JB Nizet
    Mar 23 at 16:41











  • @JBNizet Thanks, you are absolutely right. I am using H2. However in my application.yaml I have spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL, and I (maybe too naively) assumed, this suffices to make it work in a compatible way.

    – Tobias Hermann
    Mar 23 at 17:03











  • Test it with the real thing. My guess is that indeed, you were too naive :-)

    – JB Nizet
    Mar 23 at 17:04













  • 1





    In your SQL experiment, you're using PostgreSQL. In your exception, the exception is a org.h2.jdbc.JdbcSQLException, showing that you're not using PostgreSQL, but H2. My guess is that your actual production database is PostgreSQL. I would thus run tests against PostgreSQL, not H2.

    – JB Nizet
    Mar 23 at 16:41











  • @JBNizet Thanks, you are absolutely right. I am using H2. However in my application.yaml I have spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL, and I (maybe too naively) assumed, this suffices to make it work in a compatible way.

    – Tobias Hermann
    Mar 23 at 17:03











  • Test it with the real thing. My guess is that indeed, you were too naive :-)

    – JB Nizet
    Mar 23 at 17:04








1




1





In your SQL experiment, you're using PostgreSQL. In your exception, the exception is a org.h2.jdbc.JdbcSQLException, showing that you're not using PostgreSQL, but H2. My guess is that your actual production database is PostgreSQL. I would thus run tests against PostgreSQL, not H2.

– JB Nizet
Mar 23 at 16:41





In your SQL experiment, you're using PostgreSQL. In your exception, the exception is a org.h2.jdbc.JdbcSQLException, showing that you're not using PostgreSQL, but H2. My guess is that your actual production database is PostgreSQL. I would thus run tests against PostgreSQL, not H2.

– JB Nizet
Mar 23 at 16:41













@JBNizet Thanks, you are absolutely right. I am using H2. However in my application.yaml I have spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL, and I (maybe too naively) assumed, this suffices to make it work in a compatible way.

– Tobias Hermann
Mar 23 at 17:03





@JBNizet Thanks, you are absolutely right. I am using H2. However in my application.yaml I have spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL, and I (maybe too naively) assumed, this suffices to make it work in a compatible way.

– Tobias Hermann
Mar 23 at 17:03













Test it with the real thing. My guess is that indeed, you were too naive :-)

– JB Nizet
Mar 23 at 17:04






Test it with the real thing. My guess is that indeed, you were too naive :-)

– JB Nizet
Mar 23 at 17:04













1 Answer
1






active

oldest

votes


















1














Thanks to this comment by JB Nizet, I was able to solve it.



The syntax used



SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


is only supported by real PostgreSQL, not by H2, which I was using for my tests, even with



spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL


Changing it to



SELECT * FROM things WHERE (foo, bar) IN ((1, 2), (3, 4));


makes it work in both, real Postgres and H2 in Postgres mode.




Another option is to switch to embedded-database-spring-test and use it's embedded Postgres for testing. Then the original syntax (using VALUES) also works.






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%2f55315775%2fnative-query-in-spring-boot-with-where-condition-for-tuple-in-list%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














    Thanks to this comment by JB Nizet, I was able to solve it.



    The syntax used



    SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


    is only supported by real PostgreSQL, not by H2, which I was using for my tests, even with



    spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL


    Changing it to



    SELECT * FROM things WHERE (foo, bar) IN ((1, 2), (3, 4));


    makes it work in both, real Postgres and H2 in Postgres mode.




    Another option is to switch to embedded-database-spring-test and use it's embedded Postgres for testing. Then the original syntax (using VALUES) also works.






    share|improve this answer





























      1














      Thanks to this comment by JB Nizet, I was able to solve it.



      The syntax used



      SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


      is only supported by real PostgreSQL, not by H2, which I was using for my tests, even with



      spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL


      Changing it to



      SELECT * FROM things WHERE (foo, bar) IN ((1, 2), (3, 4));


      makes it work in both, real Postgres and H2 in Postgres mode.




      Another option is to switch to embedded-database-spring-test and use it's embedded Postgres for testing. Then the original syntax (using VALUES) also works.






      share|improve this answer



























        1












        1








        1







        Thanks to this comment by JB Nizet, I was able to solve it.



        The syntax used



        SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


        is only supported by real PostgreSQL, not by H2, which I was using for my tests, even with



        spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL


        Changing it to



        SELECT * FROM things WHERE (foo, bar) IN ((1, 2), (3, 4));


        makes it work in both, real Postgres and H2 in Postgres mode.




        Another option is to switch to embedded-database-spring-test and use it's embedded Postgres for testing. Then the original syntax (using VALUES) also works.






        share|improve this answer















        Thanks to this comment by JB Nizet, I was able to solve it.



        The syntax used



        SELECT * FROM things WHERE (foo, bar) IN (VALUES (1, 2), (3, 4));


        is only supported by real PostgreSQL, not by H2, which I was using for my tests, even with



        spring.datasource.url: jdbc:h2:mem:db;MODE=PostgreSQL


        Changing it to



        SELECT * FROM things WHERE (foo, bar) IN ((1, 2), (3, 4));


        makes it work in both, real Postgres and H2 in Postgres mode.




        Another option is to switch to embedded-database-spring-test and use it's embedded Postgres for testing. Then the original syntax (using VALUES) also works.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 23 at 18:16

























        answered Mar 23 at 18:03









        Tobias HermannTobias Hermann

        3,04022457




        3,04022457





























            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%2f55315775%2fnative-query-in-spring-boot-with-where-condition-for-tuple-in-list%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

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript