Transforming complex postgres query into JPQL for Spring Hibernate repository












0















I have the following Postgres SQL query, in order to get alerts with the latest date:



SELECT latest_alerts.subject_id,
latest_alerts.alertconfiguration_id_id,
latest_alerts.alert_level,
latest_alerts.maxdate
FROM (SELECT subject_id,
alertconfiguration_id_id,
alert_level,
Max(date) AS maxdate
FROM alert
WHERE subject_id IN ( 'da157532-8de5-4c0c-8608-d924e670d5db', '63b99886-77c8-4784-b8f0-7ff5310f1272' )
AND alertconfiguration_id_id IN (
'6feb6b8b-6b96-4d5d-ac58-713b3cd637a0'
)
GROUP BY subject_id,
alertconfiguration_id_id,
alert_level) AS latest_alerts
INNER JOIN alert
ON alert.date = latest_alerts.maxdate
AND alert.subject_id = latest_alerts.subject_id
AND alert.alertconfiguration_id_id =
latest_alerts.alertconfiguration_id_id
AND alert.alert_level IN ( 'WARNING' )
ORDER BY latest_alerts.maxdate DESC;


This runs well on the postgres database generated by Hibernate. Note the odd id_id construction is because of an embedded key.



But I'm struggling to transform this into a JPA/JPQL query that I can use in a Spring Boot application. So far I have this:



@Query("SELECT" +
" latest_alerts.subject," +
" latest_alerts.alertConfiguration," +
" latest_alerts.alertLevel," +
" latest_alerts.max_date" +
"FROM" +
" (SELECT" +
" subject," +
" alertConfiguration," +
" alertLevel," +
" MAX(date) AS max_date" +
" FROM" +
" alert" +
" WHERE" +
" subject.id IN (:subjectIds) AND alertConfiguration.id.id IN (:alertConfigurationIds)" +
" GROUP BY" +
" subject, alertConfiguration, alertLevel) AS latest_alerts" +
" INNER JOIN" +
" alert" +
" ON" +
" alert.date = latest_alerts.max_date" +
" AND alert.subject = latest_alerts.subject" +
" AND alert.alertConfiguration = latest_alerts.alertConfiguration" +
" AND alert.alertlevel IN (:alertLevels)" +
" ORDER BY latest_alerts.date DESC")
Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
@Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
@Param("alertLevels") List<AlertLevel> alertLevels,
Pageable pageable);


But Hibernate doesn't understand what to do with this to the point where it actually throws a nullpointer while parsing this query.



Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page ournamespace.sense.repository.AlertRepository.findLatest(java.util.List,java.util.List,java.util.List,org.springframework.data.domain.Pageable)!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:63)
... many more
Caused by: java.lang.NullPointerException
at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4355)
at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:993)
at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3549)


Any idea if this kind of query is even possible? With a nullpointer thrown by Hibernate it's a bit hard to see what part of the query is the problem.










share|improve this question



























    0















    I have the following Postgres SQL query, in order to get alerts with the latest date:



    SELECT latest_alerts.subject_id,
    latest_alerts.alertconfiguration_id_id,
    latest_alerts.alert_level,
    latest_alerts.maxdate
    FROM (SELECT subject_id,
    alertconfiguration_id_id,
    alert_level,
    Max(date) AS maxdate
    FROM alert
    WHERE subject_id IN ( 'da157532-8de5-4c0c-8608-d924e670d5db', '63b99886-77c8-4784-b8f0-7ff5310f1272' )
    AND alertconfiguration_id_id IN (
    '6feb6b8b-6b96-4d5d-ac58-713b3cd637a0'
    )
    GROUP BY subject_id,
    alertconfiguration_id_id,
    alert_level) AS latest_alerts
    INNER JOIN alert
    ON alert.date = latest_alerts.maxdate
    AND alert.subject_id = latest_alerts.subject_id
    AND alert.alertconfiguration_id_id =
    latest_alerts.alertconfiguration_id_id
    AND alert.alert_level IN ( 'WARNING' )
    ORDER BY latest_alerts.maxdate DESC;


    This runs well on the postgres database generated by Hibernate. Note the odd id_id construction is because of an embedded key.



    But I'm struggling to transform this into a JPA/JPQL query that I can use in a Spring Boot application. So far I have this:



    @Query("SELECT" +
    " latest_alerts.subject," +
    " latest_alerts.alertConfiguration," +
    " latest_alerts.alertLevel," +
    " latest_alerts.max_date" +
    "FROM" +
    " (SELECT" +
    " subject," +
    " alertConfiguration," +
    " alertLevel," +
    " MAX(date) AS max_date" +
    " FROM" +
    " alert" +
    " WHERE" +
    " subject.id IN (:subjectIds) AND alertConfiguration.id.id IN (:alertConfigurationIds)" +
    " GROUP BY" +
    " subject, alertConfiguration, alertLevel) AS latest_alerts" +
    " INNER JOIN" +
    " alert" +
    " ON" +
    " alert.date = latest_alerts.max_date" +
    " AND alert.subject = latest_alerts.subject" +
    " AND alert.alertConfiguration = latest_alerts.alertConfiguration" +
    " AND alert.alertlevel IN (:alertLevels)" +
    " ORDER BY latest_alerts.date DESC")
    Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
    @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
    @Param("alertLevels") List<AlertLevel> alertLevels,
    Pageable pageable);


    But Hibernate doesn't understand what to do with this to the point where it actually throws a nullpointer while parsing this query.



    Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page ournamespace.sense.repository.AlertRepository.findLatest(java.util.List,java.util.List,java.util.List,org.springframework.data.domain.Pageable)!
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93)
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:63)
    ... many more
    Caused by: java.lang.NullPointerException
    at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4355)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:993)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3549)


    Any idea if this kind of query is even possible? With a nullpointer thrown by Hibernate it's a bit hard to see what part of the query is the problem.










    share|improve this question

























      0












      0








      0








      I have the following Postgres SQL query, in order to get alerts with the latest date:



      SELECT latest_alerts.subject_id,
      latest_alerts.alertconfiguration_id_id,
      latest_alerts.alert_level,
      latest_alerts.maxdate
      FROM (SELECT subject_id,
      alertconfiguration_id_id,
      alert_level,
      Max(date) AS maxdate
      FROM alert
      WHERE subject_id IN ( 'da157532-8de5-4c0c-8608-d924e670d5db', '63b99886-77c8-4784-b8f0-7ff5310f1272' )
      AND alertconfiguration_id_id IN (
      '6feb6b8b-6b96-4d5d-ac58-713b3cd637a0'
      )
      GROUP BY subject_id,
      alertconfiguration_id_id,
      alert_level) AS latest_alerts
      INNER JOIN alert
      ON alert.date = latest_alerts.maxdate
      AND alert.subject_id = latest_alerts.subject_id
      AND alert.alertconfiguration_id_id =
      latest_alerts.alertconfiguration_id_id
      AND alert.alert_level IN ( 'WARNING' )
      ORDER BY latest_alerts.maxdate DESC;


      This runs well on the postgres database generated by Hibernate. Note the odd id_id construction is because of an embedded key.



      But I'm struggling to transform this into a JPA/JPQL query that I can use in a Spring Boot application. So far I have this:



      @Query("SELECT" +
      " latest_alerts.subject," +
      " latest_alerts.alertConfiguration," +
      " latest_alerts.alertLevel," +
      " latest_alerts.max_date" +
      "FROM" +
      " (SELECT" +
      " subject," +
      " alertConfiguration," +
      " alertLevel," +
      " MAX(date) AS max_date" +
      " FROM" +
      " alert" +
      " WHERE" +
      " subject.id IN (:subjectIds) AND alertConfiguration.id.id IN (:alertConfigurationIds)" +
      " GROUP BY" +
      " subject, alertConfiguration, alertLevel) AS latest_alerts" +
      " INNER JOIN" +
      " alert" +
      " ON" +
      " alert.date = latest_alerts.max_date" +
      " AND alert.subject = latest_alerts.subject" +
      " AND alert.alertConfiguration = latest_alerts.alertConfiguration" +
      " AND alert.alertlevel IN (:alertLevels)" +
      " ORDER BY latest_alerts.date DESC")
      Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
      @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
      @Param("alertLevels") List<AlertLevel> alertLevels,
      Pageable pageable);


      But Hibernate doesn't understand what to do with this to the point where it actually throws a nullpointer while parsing this query.



      Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page ournamespace.sense.repository.AlertRepository.findLatest(java.util.List,java.util.List,java.util.List,org.springframework.data.domain.Pageable)!
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93)
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:63)
      ... many more
      Caused by: java.lang.NullPointerException
      at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4355)
      at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:993)
      at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3549)


      Any idea if this kind of query is even possible? With a nullpointer thrown by Hibernate it's a bit hard to see what part of the query is the problem.










      share|improve this question














      I have the following Postgres SQL query, in order to get alerts with the latest date:



      SELECT latest_alerts.subject_id,
      latest_alerts.alertconfiguration_id_id,
      latest_alerts.alert_level,
      latest_alerts.maxdate
      FROM (SELECT subject_id,
      alertconfiguration_id_id,
      alert_level,
      Max(date) AS maxdate
      FROM alert
      WHERE subject_id IN ( 'da157532-8de5-4c0c-8608-d924e670d5db', '63b99886-77c8-4784-b8f0-7ff5310f1272' )
      AND alertconfiguration_id_id IN (
      '6feb6b8b-6b96-4d5d-ac58-713b3cd637a0'
      )
      GROUP BY subject_id,
      alertconfiguration_id_id,
      alert_level) AS latest_alerts
      INNER JOIN alert
      ON alert.date = latest_alerts.maxdate
      AND alert.subject_id = latest_alerts.subject_id
      AND alert.alertconfiguration_id_id =
      latest_alerts.alertconfiguration_id_id
      AND alert.alert_level IN ( 'WARNING' )
      ORDER BY latest_alerts.maxdate DESC;


      This runs well on the postgres database generated by Hibernate. Note the odd id_id construction is because of an embedded key.



      But I'm struggling to transform this into a JPA/JPQL query that I can use in a Spring Boot application. So far I have this:



      @Query("SELECT" +
      " latest_alerts.subject," +
      " latest_alerts.alertConfiguration," +
      " latest_alerts.alertLevel," +
      " latest_alerts.max_date" +
      "FROM" +
      " (SELECT" +
      " subject," +
      " alertConfiguration," +
      " alertLevel," +
      " MAX(date) AS max_date" +
      " FROM" +
      " alert" +
      " WHERE" +
      " subject.id IN (:subjectIds) AND alertConfiguration.id.id IN (:alertConfigurationIds)" +
      " GROUP BY" +
      " subject, alertConfiguration, alertLevel) AS latest_alerts" +
      " INNER JOIN" +
      " alert" +
      " ON" +
      " alert.date = latest_alerts.max_date" +
      " AND alert.subject = latest_alerts.subject" +
      " AND alert.alertConfiguration = latest_alerts.alertConfiguration" +
      " AND alert.alertlevel IN (:alertLevels)" +
      " ORDER BY latest_alerts.date DESC")
      Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
      @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
      @Param("alertLevels") List<AlertLevel> alertLevels,
      Pageable pageable);


      But Hibernate doesn't understand what to do with this to the point where it actually throws a nullpointer while parsing this query.



      Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract org.springframework.data.domain.Page ournamespace.sense.repository.AlertRepository.findLatest(java.util.List,java.util.List,java.util.List,org.springframework.data.domain.Pageable)!
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:93)
      at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:63)
      ... many more
      Caused by: java.lang.NullPointerException
      at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4355)
      at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:993)
      at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3549)


      Any idea if this kind of query is even possible? With a nullpointer thrown by Hibernate it's a bit hard to see what part of the query is the problem.







      postgresql hibernate jpa spring-data-jpa spring-data






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 6:55









      Sebastiaan van den BroekSebastiaan van den Broek

      3,29331843




      3,29331843
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Cannot do it with Hibernate. From the documentation:




          Note that HQL subqueries can occur only in the select or where
          clauses.




          But you can use native query:



          @Query(
          value = "SELECT latest_alerts.subject_id,
          latest_alerts.alertconfiguration_id_id,
          latest_alerts.alert_level,
          latest_alerts.maxdate ...
          FROM ...",
          nativeQuery = true)
          Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
          @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
          @Param("alertLevels") List<AlertLevel> alertLevels,
          Pageable pageable);





          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%2f53425384%2ftransforming-complex-postgres-query-into-jpql-for-spring-hibernate-repository%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














            Cannot do it with Hibernate. From the documentation:




            Note that HQL subqueries can occur only in the select or where
            clauses.




            But you can use native query:



            @Query(
            value = "SELECT latest_alerts.subject_id,
            latest_alerts.alertconfiguration_id_id,
            latest_alerts.alert_level,
            latest_alerts.maxdate ...
            FROM ...",
            nativeQuery = true)
            Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
            @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
            @Param("alertLevels") List<AlertLevel> alertLevels,
            Pageable pageable);





            share|improve this answer




























              1














              Cannot do it with Hibernate. From the documentation:




              Note that HQL subqueries can occur only in the select or where
              clauses.




              But you can use native query:



              @Query(
              value = "SELECT latest_alerts.subject_id,
              latest_alerts.alertconfiguration_id_id,
              latest_alerts.alert_level,
              latest_alerts.maxdate ...
              FROM ...",
              nativeQuery = true)
              Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
              @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
              @Param("alertLevels") List<AlertLevel> alertLevels,
              Pageable pageable);





              share|improve this answer


























                1












                1








                1







                Cannot do it with Hibernate. From the documentation:




                Note that HQL subqueries can occur only in the select or where
                clauses.




                But you can use native query:



                @Query(
                value = "SELECT latest_alerts.subject_id,
                latest_alerts.alertconfiguration_id_id,
                latest_alerts.alert_level,
                latest_alerts.maxdate ...
                FROM ...",
                nativeQuery = true)
                Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
                @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
                @Param("alertLevels") List<AlertLevel> alertLevels,
                Pageable pageable);





                share|improve this answer













                Cannot do it with Hibernate. From the documentation:




                Note that HQL subqueries can occur only in the select or where
                clauses.




                But you can use native query:



                @Query(
                value = "SELECT latest_alerts.subject_id,
                latest_alerts.alertconfiguration_id_id,
                latest_alerts.alert_level,
                latest_alerts.maxdate ...
                FROM ...",
                nativeQuery = true)
                Page<Alert> findLatest(@Param("subjectIds") List<UUID> subjectIds,
                @Param("alertConfigurationIds") List<UUID> alertConfigurationIds,
                @Param("alertLevels") List<AlertLevel> alertLevels,
                Pageable pageable);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 '18 at 7:02







                user10639668





































                    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%2f53425384%2ftransforming-complex-postgres-query-into-jpql-for-spring-hibernate-repository%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python