Suppress error message No entity found for query





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







1















I use this code to get single row result:



@Override
public PaymentTransactions transactionByUnique_Id(String unique_id) throws Exception {

String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.unique_id = :unique_id";
Query query = entityManager.createQuery(hql).setParameter("unique_id", unique_id);
PaymentTransactions paymentTransaction = (PaymentTransactions) query.getSingleResult();
return paymentTransaction;
}


But when I don't have matching row I get exception:



14:25:41,736 INFO  [stdout] (default task-1) javax.persistence.NoResultException: No entity found for query


How I can suppress that exception and just to continue code execution?










share|improve this question























  • Call getResultList(), and return its first element if it's present. Also, please, respect the Java naming conventions, and use a TypedQuery.

    – JB Nizet
    Nov 24 '18 at 12:31













  • How should I change the code in order to use TypedQuery?

    – Peter Penzov
    Nov 24 '18 at 12:36






  • 1





    docs.oracle.com/javaee/7/api/javax/persistence/…

    – JB Nizet
    Nov 24 '18 at 12:38











  • Basic java is catching an exception and returning a different result from your method based on that. Nothing to do with the JPA API

    – user3973283
    Nov 24 '18 at 17:29


















1















I use this code to get single row result:



@Override
public PaymentTransactions transactionByUnique_Id(String unique_id) throws Exception {

String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.unique_id = :unique_id";
Query query = entityManager.createQuery(hql).setParameter("unique_id", unique_id);
PaymentTransactions paymentTransaction = (PaymentTransactions) query.getSingleResult();
return paymentTransaction;
}


But when I don't have matching row I get exception:



14:25:41,736 INFO  [stdout] (default task-1) javax.persistence.NoResultException: No entity found for query


How I can suppress that exception and just to continue code execution?










share|improve this question























  • Call getResultList(), and return its first element if it's present. Also, please, respect the Java naming conventions, and use a TypedQuery.

    – JB Nizet
    Nov 24 '18 at 12:31













  • How should I change the code in order to use TypedQuery?

    – Peter Penzov
    Nov 24 '18 at 12:36






  • 1





    docs.oracle.com/javaee/7/api/javax/persistence/…

    – JB Nizet
    Nov 24 '18 at 12:38











  • Basic java is catching an exception and returning a different result from your method based on that. Nothing to do with the JPA API

    – user3973283
    Nov 24 '18 at 17:29














1












1








1








I use this code to get single row result:



@Override
public PaymentTransactions transactionByUnique_Id(String unique_id) throws Exception {

String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.unique_id = :unique_id";
Query query = entityManager.createQuery(hql).setParameter("unique_id", unique_id);
PaymentTransactions paymentTransaction = (PaymentTransactions) query.getSingleResult();
return paymentTransaction;
}


But when I don't have matching row I get exception:



14:25:41,736 INFO  [stdout] (default task-1) javax.persistence.NoResultException: No entity found for query


How I can suppress that exception and just to continue code execution?










share|improve this question














I use this code to get single row result:



@Override
public PaymentTransactions transactionByUnique_Id(String unique_id) throws Exception {

String hql = "select e from " + PaymentTransactions.class.getName() + " e where e.unique_id = :unique_id";
Query query = entityManager.createQuery(hql).setParameter("unique_id", unique_id);
PaymentTransactions paymentTransaction = (PaymentTransactions) query.getSingleResult();
return paymentTransaction;
}


But when I don't have matching row I get exception:



14:25:41,736 INFO  [stdout] (default task-1) javax.persistence.NoResultException: No entity found for query


How I can suppress that exception and just to continue code execution?







java jpa spring-data






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 12:29









Peter PenzovPeter Penzov

27461191399




27461191399













  • Call getResultList(), and return its first element if it's present. Also, please, respect the Java naming conventions, and use a TypedQuery.

    – JB Nizet
    Nov 24 '18 at 12:31













  • How should I change the code in order to use TypedQuery?

    – Peter Penzov
    Nov 24 '18 at 12:36






  • 1





    docs.oracle.com/javaee/7/api/javax/persistence/…

    – JB Nizet
    Nov 24 '18 at 12:38











  • Basic java is catching an exception and returning a different result from your method based on that. Nothing to do with the JPA API

    – user3973283
    Nov 24 '18 at 17:29



















  • Call getResultList(), and return its first element if it's present. Also, please, respect the Java naming conventions, and use a TypedQuery.

    – JB Nizet
    Nov 24 '18 at 12:31













  • How should I change the code in order to use TypedQuery?

    – Peter Penzov
    Nov 24 '18 at 12:36






  • 1





    docs.oracle.com/javaee/7/api/javax/persistence/…

    – JB Nizet
    Nov 24 '18 at 12:38











  • Basic java is catching an exception and returning a different result from your method based on that. Nothing to do with the JPA API

    – user3973283
    Nov 24 '18 at 17:29

















Call getResultList(), and return its first element if it's present. Also, please, respect the Java naming conventions, and use a TypedQuery.

– JB Nizet
Nov 24 '18 at 12:31







Call getResultList(), and return its first element if it's present. Also, please, respect the Java naming conventions, and use a TypedQuery.

– JB Nizet
Nov 24 '18 at 12:31















How should I change the code in order to use TypedQuery?

– Peter Penzov
Nov 24 '18 at 12:36





How should I change the code in order to use TypedQuery?

– Peter Penzov
Nov 24 '18 at 12:36




1




1





docs.oracle.com/javaee/7/api/javax/persistence/…

– JB Nizet
Nov 24 '18 at 12:38





docs.oracle.com/javaee/7/api/javax/persistence/…

– JB Nizet
Nov 24 '18 at 12:38













Basic java is catching an exception and returning a different result from your method based on that. Nothing to do with the JPA API

– user3973283
Nov 24 '18 at 17:29





Basic java is catching an exception and returning a different result from your method based on that. Nothing to do with the JPA API

– user3973283
Nov 24 '18 at 17:29












2 Answers
2






active

oldest

votes


















3














Just throw and catch javax.persistence.NoResultException exception and continue your code execution.



Possible duplicate of How to continue program execution even after throwing exception?






share|improve this answer































    1














    Just update way to logging:



        log4j.logger.javax.persistence=WARN





    share|improve this answer


























    • ok, but I don't want to get Java exception.

      – Peter Penzov
      Nov 24 '18 at 14:53











    • I updated it, could you try it?

      – Jonathan Johx
      Nov 24 '18 at 15:16












    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%2f53458151%2fsuppress-error-message-no-entity-found-for-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Just throw and catch javax.persistence.NoResultException exception and continue your code execution.



    Possible duplicate of How to continue program execution even after throwing exception?






    share|improve this answer




























      3














      Just throw and catch javax.persistence.NoResultException exception and continue your code execution.



      Possible duplicate of How to continue program execution even after throwing exception?






      share|improve this answer


























        3












        3








        3







        Just throw and catch javax.persistence.NoResultException exception and continue your code execution.



        Possible duplicate of How to continue program execution even after throwing exception?






        share|improve this answer













        Just throw and catch javax.persistence.NoResultException exception and continue your code execution.



        Possible duplicate of How to continue program execution even after throwing exception?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 16:47









        Ravi SharmaRavi Sharma

        1814




        1814

























            1














            Just update way to logging:



                log4j.logger.javax.persistence=WARN





            share|improve this answer


























            • ok, but I don't want to get Java exception.

              – Peter Penzov
              Nov 24 '18 at 14:53











            • I updated it, could you try it?

              – Jonathan Johx
              Nov 24 '18 at 15:16
















            1














            Just update way to logging:



                log4j.logger.javax.persistence=WARN





            share|improve this answer


























            • ok, but I don't want to get Java exception.

              – Peter Penzov
              Nov 24 '18 at 14:53











            • I updated it, could you try it?

              – Jonathan Johx
              Nov 24 '18 at 15:16














            1












            1








            1







            Just update way to logging:



                log4j.logger.javax.persistence=WARN





            share|improve this answer















            Just update way to logging:



                log4j.logger.javax.persistence=WARN






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 '18 at 15:15

























            answered Nov 24 '18 at 13:57









            Jonathan JohxJonathan Johx

            1




            1













            • ok, but I don't want to get Java exception.

              – Peter Penzov
              Nov 24 '18 at 14:53











            • I updated it, could you try it?

              – Jonathan Johx
              Nov 24 '18 at 15:16



















            • ok, but I don't want to get Java exception.

              – Peter Penzov
              Nov 24 '18 at 14:53











            • I updated it, could you try it?

              – Jonathan Johx
              Nov 24 '18 at 15:16

















            ok, but I don't want to get Java exception.

            – Peter Penzov
            Nov 24 '18 at 14:53





            ok, but I don't want to get Java exception.

            – Peter Penzov
            Nov 24 '18 at 14:53













            I updated it, could you try it?

            – Jonathan Johx
            Nov 24 '18 at 15:16





            I updated it, could you try it?

            – Jonathan Johx
            Nov 24 '18 at 15:16


















            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%2f53458151%2fsuppress-error-message-no-entity-found-for-query%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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings