Specific SQL to JPQL query convert










-2















I am looking for the equivalent of this SQL query in JPQL



SELECT l.*
FROM product_update l
INNER JOIN (
SELECT
ID_PRODUCT,
MAX(ID) AS maxID
FROM product_update
GROUP BY ID_PRODUCT
) groupel ON l.ID_PRODUCT = groupel.ID_PRODUCT
AND l.ID = groupel.maxID
ORDER BY ID DESC


My java class



public class ProductUpdate extends BaseEntity 

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PRODUCT_UPDATE_SEQUENCE")
@Column(name = "ID")
private Long id;

@JoinColumn(name = "ID_PRODUCT")
@ManyToOne()
private Product product;


@JoinColumn(name = "ID_VERSION")
@ManyToOne()
private Versioning versioning;



indeed, a product may have several versions, but the system just displays the product with the max (id)
Example:



1-Product 1 --- Version 1 
2-Product 2 --- Version 1
3-Product 3 --- Version 1
4-Product 1 --- Version 2
5-Product 3 --- Version 2
6-Product 3 --- Version 3


that the query just comes out



2-Product 2 --- Version 1
4-Product 1 --- Version 2
6-Product 3 --- Version 3


Thank.










share|improve this question
























  • Have you tried to make one yourself, or are you expecting someone here to do your work for you? Please show us your attempt.

    – Tsahi Asher
    Nov 15 '18 at 15:54











  • Since JPQL is based around classes/fields and you haven't posted any classes, why not have a think what people are likely to contribute to that?!

    – Billy Frost
    Nov 15 '18 at 16:40











  • and guess what ... you STILL HAVE NOT POSTED THE CLASSES (in your question!)

    – Billy Frost
    Nov 15 '18 at 18:36











  • This what i have done SELECT pu FROM ProductUpdate pu INNER JOIN ( SELECT pu.product, MAX(pu.id) AS maxid FROM ProductUpdate pu GROUP BY pu.product ) groupepu ON pu.product = groupepu.produit AND pu.id = groupepu.maxID ORDER BY pu.id DESC. But it shows me this warning on netbeans the joint association path is not a valid expression. Here is my Table ProductUpdate (Id, Name FK Product)

    – DERICK TERRICK
    Nov 15 '18 at 19:22











  • ^ Please add your attempt in the question itself, using a formatted block.

    – halfer
    Nov 15 '18 at 22:06
















-2















I am looking for the equivalent of this SQL query in JPQL



SELECT l.*
FROM product_update l
INNER JOIN (
SELECT
ID_PRODUCT,
MAX(ID) AS maxID
FROM product_update
GROUP BY ID_PRODUCT
) groupel ON l.ID_PRODUCT = groupel.ID_PRODUCT
AND l.ID = groupel.maxID
ORDER BY ID DESC


My java class



public class ProductUpdate extends BaseEntity 

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PRODUCT_UPDATE_SEQUENCE")
@Column(name = "ID")
private Long id;

@JoinColumn(name = "ID_PRODUCT")
@ManyToOne()
private Product product;


@JoinColumn(name = "ID_VERSION")
@ManyToOne()
private Versioning versioning;



indeed, a product may have several versions, but the system just displays the product with the max (id)
Example:



1-Product 1 --- Version 1 
2-Product 2 --- Version 1
3-Product 3 --- Version 1
4-Product 1 --- Version 2
5-Product 3 --- Version 2
6-Product 3 --- Version 3


that the query just comes out



2-Product 2 --- Version 1
4-Product 1 --- Version 2
6-Product 3 --- Version 3


Thank.










share|improve this question
























  • Have you tried to make one yourself, or are you expecting someone here to do your work for you? Please show us your attempt.

    – Tsahi Asher
    Nov 15 '18 at 15:54











  • Since JPQL is based around classes/fields and you haven't posted any classes, why not have a think what people are likely to contribute to that?!

    – Billy Frost
    Nov 15 '18 at 16:40











  • and guess what ... you STILL HAVE NOT POSTED THE CLASSES (in your question!)

    – Billy Frost
    Nov 15 '18 at 18:36











  • This what i have done SELECT pu FROM ProductUpdate pu INNER JOIN ( SELECT pu.product, MAX(pu.id) AS maxid FROM ProductUpdate pu GROUP BY pu.product ) groupepu ON pu.product = groupepu.produit AND pu.id = groupepu.maxID ORDER BY pu.id DESC. But it shows me this warning on netbeans the joint association path is not a valid expression. Here is my Table ProductUpdate (Id, Name FK Product)

    – DERICK TERRICK
    Nov 15 '18 at 19:22











  • ^ Please add your attempt in the question itself, using a formatted block.

    – halfer
    Nov 15 '18 at 22:06














-2












-2








-2








I am looking for the equivalent of this SQL query in JPQL



SELECT l.*
FROM product_update l
INNER JOIN (
SELECT
ID_PRODUCT,
MAX(ID) AS maxID
FROM product_update
GROUP BY ID_PRODUCT
) groupel ON l.ID_PRODUCT = groupel.ID_PRODUCT
AND l.ID = groupel.maxID
ORDER BY ID DESC


My java class



public class ProductUpdate extends BaseEntity 

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PRODUCT_UPDATE_SEQUENCE")
@Column(name = "ID")
private Long id;

@JoinColumn(name = "ID_PRODUCT")
@ManyToOne()
private Product product;


@JoinColumn(name = "ID_VERSION")
@ManyToOne()
private Versioning versioning;



indeed, a product may have several versions, but the system just displays the product with the max (id)
Example:



1-Product 1 --- Version 1 
2-Product 2 --- Version 1
3-Product 3 --- Version 1
4-Product 1 --- Version 2
5-Product 3 --- Version 2
6-Product 3 --- Version 3


that the query just comes out



2-Product 2 --- Version 1
4-Product 1 --- Version 2
6-Product 3 --- Version 3


Thank.










share|improve this question
















I am looking for the equivalent of this SQL query in JPQL



SELECT l.*
FROM product_update l
INNER JOIN (
SELECT
ID_PRODUCT,
MAX(ID) AS maxID
FROM product_update
GROUP BY ID_PRODUCT
) groupel ON l.ID_PRODUCT = groupel.ID_PRODUCT
AND l.ID = groupel.maxID
ORDER BY ID DESC


My java class



public class ProductUpdate extends BaseEntity 

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PRODUCT_UPDATE_SEQUENCE")
@Column(name = "ID")
private Long id;

@JoinColumn(name = "ID_PRODUCT")
@ManyToOne()
private Product product;


@JoinColumn(name = "ID_VERSION")
@ManyToOne()
private Versioning versioning;



indeed, a product may have several versions, but the system just displays the product with the max (id)
Example:



1-Product 1 --- Version 1 
2-Product 2 --- Version 1
3-Product 3 --- Version 1
4-Product 1 --- Version 2
5-Product 3 --- Version 2
6-Product 3 --- Version 3


that the query just comes out



2-Product 2 --- Version 1
4-Product 1 --- Version 2
6-Product 3 --- Version 3


Thank.







jpa jpql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 10:28







DERICK TERRICK

















asked Nov 15 '18 at 15:46









DERICK TERRICKDERICK TERRICK

11




11












  • Have you tried to make one yourself, or are you expecting someone here to do your work for you? Please show us your attempt.

    – Tsahi Asher
    Nov 15 '18 at 15:54











  • Since JPQL is based around classes/fields and you haven't posted any classes, why not have a think what people are likely to contribute to that?!

    – Billy Frost
    Nov 15 '18 at 16:40











  • and guess what ... you STILL HAVE NOT POSTED THE CLASSES (in your question!)

    – Billy Frost
    Nov 15 '18 at 18:36











  • This what i have done SELECT pu FROM ProductUpdate pu INNER JOIN ( SELECT pu.product, MAX(pu.id) AS maxid FROM ProductUpdate pu GROUP BY pu.product ) groupepu ON pu.product = groupepu.produit AND pu.id = groupepu.maxID ORDER BY pu.id DESC. But it shows me this warning on netbeans the joint association path is not a valid expression. Here is my Table ProductUpdate (Id, Name FK Product)

    – DERICK TERRICK
    Nov 15 '18 at 19:22











  • ^ Please add your attempt in the question itself, using a formatted block.

    – halfer
    Nov 15 '18 at 22:06


















  • Have you tried to make one yourself, or are you expecting someone here to do your work for you? Please show us your attempt.

    – Tsahi Asher
    Nov 15 '18 at 15:54











  • Since JPQL is based around classes/fields and you haven't posted any classes, why not have a think what people are likely to contribute to that?!

    – Billy Frost
    Nov 15 '18 at 16:40











  • and guess what ... you STILL HAVE NOT POSTED THE CLASSES (in your question!)

    – Billy Frost
    Nov 15 '18 at 18:36











  • This what i have done SELECT pu FROM ProductUpdate pu INNER JOIN ( SELECT pu.product, MAX(pu.id) AS maxid FROM ProductUpdate pu GROUP BY pu.product ) groupepu ON pu.product = groupepu.produit AND pu.id = groupepu.maxID ORDER BY pu.id DESC. But it shows me this warning on netbeans the joint association path is not a valid expression. Here is my Table ProductUpdate (Id, Name FK Product)

    – DERICK TERRICK
    Nov 15 '18 at 19:22











  • ^ Please add your attempt in the question itself, using a formatted block.

    – halfer
    Nov 15 '18 at 22:06

















Have you tried to make one yourself, or are you expecting someone here to do your work for you? Please show us your attempt.

– Tsahi Asher
Nov 15 '18 at 15:54





Have you tried to make one yourself, or are you expecting someone here to do your work for you? Please show us your attempt.

– Tsahi Asher
Nov 15 '18 at 15:54













Since JPQL is based around classes/fields and you haven't posted any classes, why not have a think what people are likely to contribute to that?!

– Billy Frost
Nov 15 '18 at 16:40





Since JPQL is based around classes/fields and you haven't posted any classes, why not have a think what people are likely to contribute to that?!

– Billy Frost
Nov 15 '18 at 16:40













and guess what ... you STILL HAVE NOT POSTED THE CLASSES (in your question!)

– Billy Frost
Nov 15 '18 at 18:36





and guess what ... you STILL HAVE NOT POSTED THE CLASSES (in your question!)

– Billy Frost
Nov 15 '18 at 18:36













This what i have done SELECT pu FROM ProductUpdate pu INNER JOIN ( SELECT pu.product, MAX(pu.id) AS maxid FROM ProductUpdate pu GROUP BY pu.product ) groupepu ON pu.product = groupepu.produit AND pu.id = groupepu.maxID ORDER BY pu.id DESC. But it shows me this warning on netbeans the joint association path is not a valid expression. Here is my Table ProductUpdate (Id, Name FK Product)

– DERICK TERRICK
Nov 15 '18 at 19:22





This what i have done SELECT pu FROM ProductUpdate pu INNER JOIN ( SELECT pu.product, MAX(pu.id) AS maxid FROM ProductUpdate pu GROUP BY pu.product ) groupepu ON pu.product = groupepu.produit AND pu.id = groupepu.maxID ORDER BY pu.id DESC. But it shows me this warning on netbeans the joint association path is not a valid expression. Here is my Table ProductUpdate (Id, Name FK Product)

– DERICK TERRICK
Nov 15 '18 at 19:22













^ Please add your attempt in the question itself, using a formatted block.

– halfer
Nov 15 '18 at 22:06






^ Please add your attempt in the question itself, using a formatted block.

– halfer
Nov 15 '18 at 22:06













1 Answer
1






active

oldest

votes


















0














I finally got the good query,



select pu from ProductUpdate pu where pu.id in (select MAX(pUd.id) from ProductUpdate pUd group by pUd.product.id) ORDER BY pu.id DESC. 


I just use this post JPA CriteriaBuilder for join in subquery to perform my answer.



  1. Using a JPA subquery only positions in the WHERE, hence the Netbeans warning

  2. INNER JOIN in JPA doesn't accept a subquery.





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%2f53323055%2fspecific-sql-to-jpql-query-convert%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I finally got the good query,



    select pu from ProductUpdate pu where pu.id in (select MAX(pUd.id) from ProductUpdate pUd group by pUd.product.id) ORDER BY pu.id DESC. 


    I just use this post JPA CriteriaBuilder for join in subquery to perform my answer.



    1. Using a JPA subquery only positions in the WHERE, hence the Netbeans warning

    2. INNER JOIN in JPA doesn't accept a subquery.





    share|improve this answer





























      0














      I finally got the good query,



      select pu from ProductUpdate pu where pu.id in (select MAX(pUd.id) from ProductUpdate pUd group by pUd.product.id) ORDER BY pu.id DESC. 


      I just use this post JPA CriteriaBuilder for join in subquery to perform my answer.



      1. Using a JPA subquery only positions in the WHERE, hence the Netbeans warning

      2. INNER JOIN in JPA doesn't accept a subquery.





      share|improve this answer



























        0












        0








        0







        I finally got the good query,



        select pu from ProductUpdate pu where pu.id in (select MAX(pUd.id) from ProductUpdate pUd group by pUd.product.id) ORDER BY pu.id DESC. 


        I just use this post JPA CriteriaBuilder for join in subquery to perform my answer.



        1. Using a JPA subquery only positions in the WHERE, hence the Netbeans warning

        2. INNER JOIN in JPA doesn't accept a subquery.





        share|improve this answer















        I finally got the good query,



        select pu from ProductUpdate pu where pu.id in (select MAX(pUd.id) from ProductUpdate pUd group by pUd.product.id) ORDER BY pu.id DESC. 


        I just use this post JPA CriteriaBuilder for join in subquery to perform my answer.



        1. Using a JPA subquery only positions in the WHERE, hence the Netbeans warning

        2. INNER JOIN in JPA doesn't accept a subquery.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        answered Dec 20 '18 at 14:28


























        community wiki





        Dherik






























            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%2f53323055%2fspecific-sql-to-jpql-query-convert%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党