How to fetch an entity having a timestamp property that is within an hour from other timestamp property with...












0















I have a doubt in JPA query criteria builder with path or expression.



I want to find who are all entered first post comment with in an hour after their  account created



Path<Date> accountCreatedTime = root.<Date> get("AccountCreatedTime");
Path<Date> firstPostCreatedTime = root.<Date> get("FirstPostCreatedTime");

final Predicate timeInHourPredicate = criteriaBuilder
.greaterThanOrEqualTo(accountCreatedTime, FirstPostCreatedTime);


Example:




  1. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 10.15 am  this recond should be fetched. (FALLS IN AN HOUR)


  2. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 3.50 pm  this SHOULD NOT  be fetched.



Is there any  way to add or separate hours from Path accountCreatedTime? or can we get difference between   Path accountCreatedTime  and Path FirstPostCreatedTime  in hours and in criteriaBuilder










share|improve this question





























    0















    I have a doubt in JPA query criteria builder with path or expression.



    I want to find who are all entered first post comment with in an hour after their  account created



    Path<Date> accountCreatedTime = root.<Date> get("AccountCreatedTime");
    Path<Date> firstPostCreatedTime = root.<Date> get("FirstPostCreatedTime");

    final Predicate timeInHourPredicate = criteriaBuilder
    .greaterThanOrEqualTo(accountCreatedTime, FirstPostCreatedTime);


    Example:




    1. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 10.15 am  this recond should be fetched. (FALLS IN AN HOUR)


    2. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 3.50 pm  this SHOULD NOT  be fetched.



    Is there any  way to add or separate hours from Path accountCreatedTime? or can we get difference between   Path accountCreatedTime  and Path FirstPostCreatedTime  in hours and in criteriaBuilder










    share|improve this question



























      0












      0








      0


      1






      I have a doubt in JPA query criteria builder with path or expression.



      I want to find who are all entered first post comment with in an hour after their  account created



      Path<Date> accountCreatedTime = root.<Date> get("AccountCreatedTime");
      Path<Date> firstPostCreatedTime = root.<Date> get("FirstPostCreatedTime");

      final Predicate timeInHourPredicate = criteriaBuilder
      .greaterThanOrEqualTo(accountCreatedTime, FirstPostCreatedTime);


      Example:




      1. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 10.15 am  this recond should be fetched. (FALLS IN AN HOUR)


      2. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 3.50 pm  this SHOULD NOT  be fetched.



      Is there any  way to add or separate hours from Path accountCreatedTime? or can we get difference between   Path accountCreatedTime  and Path FirstPostCreatedTime  in hours and in criteriaBuilder










      share|improve this question
















      I have a doubt in JPA query criteria builder with path or expression.



      I want to find who are all entered first post comment with in an hour after their  account created



      Path<Date> accountCreatedTime = root.<Date> get("AccountCreatedTime");
      Path<Date> firstPostCreatedTime = root.<Date> get("FirstPostCreatedTime");

      final Predicate timeInHourPredicate = criteriaBuilder
      .greaterThanOrEqualTo(accountCreatedTime, FirstPostCreatedTime);


      Example:




      1. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 10.15 am  this recond should be fetched. (FALLS IN AN HOUR)


      2. Account Created at: 2018-SEP-10 at 10am and First Post entered 2018-SEP-10 at 3.50 pm  this SHOULD NOT  be fetched.



      Is there any  way to add or separate hours from Path accountCreatedTime? or can we get difference between   Path accountCreatedTime  and Path FirstPostCreatedTime  in hours and in criteriaBuilder







      hibernate jpa spring-data-jpa jpa-2.1 hibernate-jpa






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 14:16









      Vlad Mihalcea

      57.5k13156461




      57.5k13156461










      asked Jan 20 at 3:18









      sankar singhsankar singh

      6




      6
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Using DATEDIFF



          The SQL would be like this:



          select u
          from users u
          join posts p on p.user_id = u.id
          where datediff('hour', u.account_created_time, p.first_post_created_time)


          For more details about how you can register the DATEDIFF function with Criteria API, check out this article.






          share|improve this answer


























          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder

            – sankar singh
            Jan 20 at 14:12













          • Just translate the SQL query to Criteria API and it should work. If you want to do it with greaterThanOrEqualTo, you need to queries, and that's less efficient.

            – Vlad Mihalcea
            Jan 20 at 14:14











          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder. Pls assume first post created time and account created time columns are in the same.table.

            – sankar singh
            Jan 20 at 14:26











          • If you use DATEDIFF, it will work.

            – Vlad Mihalcea
            Jan 20 at 18:47











          • Yes, I tried, like this. MS Sql Server ' String functionName="Datediff" Predicate predicate=cb.greaterThan(builder.function(functionName,Integer.class,builder.literal("MINUTE"),PostEnteredDateTM, accountCreatedTM), 60 );' Error is : Invalid parameter 1 specified for datediff.

            – sankar singh
            Jan 21 at 19:02













          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%2f54273310%2fhow-to-fetch-an-entity-having-a-timestamp-property-that-is-within-an-hour-from-o%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














          Using DATEDIFF



          The SQL would be like this:



          select u
          from users u
          join posts p on p.user_id = u.id
          where datediff('hour', u.account_created_time, p.first_post_created_time)


          For more details about how you can register the DATEDIFF function with Criteria API, check out this article.






          share|improve this answer


























          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder

            – sankar singh
            Jan 20 at 14:12













          • Just translate the SQL query to Criteria API and it should work. If you want to do it with greaterThanOrEqualTo, you need to queries, and that's less efficient.

            – Vlad Mihalcea
            Jan 20 at 14:14











          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder. Pls assume first post created time and account created time columns are in the same.table.

            – sankar singh
            Jan 20 at 14:26











          • If you use DATEDIFF, it will work.

            – Vlad Mihalcea
            Jan 20 at 18:47











          • Yes, I tried, like this. MS Sql Server ' String functionName="Datediff" Predicate predicate=cb.greaterThan(builder.function(functionName,Integer.class,builder.literal("MINUTE"),PostEnteredDateTM, accountCreatedTM), 60 );' Error is : Invalid parameter 1 specified for datediff.

            – sankar singh
            Jan 21 at 19:02


















          0














          Using DATEDIFF



          The SQL would be like this:



          select u
          from users u
          join posts p on p.user_id = u.id
          where datediff('hour', u.account_created_time, p.first_post_created_time)


          For more details about how you can register the DATEDIFF function with Criteria API, check out this article.






          share|improve this answer


























          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder

            – sankar singh
            Jan 20 at 14:12













          • Just translate the SQL query to Criteria API and it should work. If you want to do it with greaterThanOrEqualTo, you need to queries, and that's less efficient.

            – Vlad Mihalcea
            Jan 20 at 14:14











          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder. Pls assume first post created time and account created time columns are in the same.table.

            – sankar singh
            Jan 20 at 14:26











          • If you use DATEDIFF, it will work.

            – Vlad Mihalcea
            Jan 20 at 18:47











          • Yes, I tried, like this. MS Sql Server ' String functionName="Datediff" Predicate predicate=cb.greaterThan(builder.function(functionName,Integer.class,builder.literal("MINUTE"),PostEnteredDateTM, accountCreatedTM), 60 );' Error is : Invalid parameter 1 specified for datediff.

            – sankar singh
            Jan 21 at 19:02
















          0












          0








          0







          Using DATEDIFF



          The SQL would be like this:



          select u
          from users u
          join posts p on p.user_id = u.id
          where datediff('hour', u.account_created_time, p.first_post_created_time)


          For more details about how you can register the DATEDIFF function with Criteria API, check out this article.






          share|improve this answer















          Using DATEDIFF



          The SQL would be like this:



          select u
          from users u
          join posts p on p.user_id = u.id
          where datediff('hour', u.account_created_time, p.first_post_created_time)


          For more details about how you can register the DATEDIFF function with Criteria API, check out this article.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 20 at 14:13

























          answered Jan 20 at 14:05









          Vlad MihalceaVlad Mihalcea

          57.5k13156461




          57.5k13156461













          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder

            – sankar singh
            Jan 20 at 14:12













          • Just translate the SQL query to Criteria API and it should work. If you want to do it with greaterThanOrEqualTo, you need to queries, and that's less efficient.

            – Vlad Mihalcea
            Jan 20 at 14:14











          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder. Pls assume first post created time and account created time columns are in the same.table.

            – sankar singh
            Jan 20 at 14:26











          • If you use DATEDIFF, it will work.

            – Vlad Mihalcea
            Jan 20 at 18:47











          • Yes, I tried, like this. MS Sql Server ' String functionName="Datediff" Predicate predicate=cb.greaterThan(builder.function(functionName,Integer.class,builder.literal("MINUTE"),PostEnteredDateTM, accountCreatedTM), 60 );' Error is : Invalid parameter 1 specified for datediff.

            – sankar singh
            Jan 21 at 19:02





















          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder

            – sankar singh
            Jan 20 at 14:12













          • Just translate the SQL query to Criteria API and it should work. If you want to do it with greaterThanOrEqualTo, you need to queries, and that's less efficient.

            – Vlad Mihalcea
            Jan 20 at 14:14











          • Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder. Pls assume first post created time and account created time columns are in the same.table.

            – sankar singh
            Jan 20 at 14:26











          • If you use DATEDIFF, it will work.

            – Vlad Mihalcea
            Jan 20 at 18:47











          • Yes, I tried, like this. MS Sql Server ' String functionName="Datediff" Predicate predicate=cb.greaterThan(builder.function(functionName,Integer.class,builder.literal("MINUTE"),PostEnteredDateTM, accountCreatedTM), 60 );' Error is : Invalid parameter 1 specified for datediff.

            – sankar singh
            Jan 21 at 19:02



















          Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder

          – sankar singh
          Jan 20 at 14:12







          Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder

          – sankar singh
          Jan 20 at 14:12















          Just translate the SQL query to Criteria API and it should work. If you want to do it with greaterThanOrEqualTo, you need to queries, and that's less efficient.

          – Vlad Mihalcea
          Jan 20 at 14:14





          Just translate the SQL query to Criteria API and it should work. If you want to do it with greaterThanOrEqualTo, you need to queries, and that's less efficient.

          – Vlad Mihalcea
          Jan 20 at 14:14













          Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder. Pls assume first post created time and account created time columns are in the same.table.

          – sankar singh
          Jan 20 at 14:26





          Path accountCreatedTime = root. get("AccountCreatedTime"); Path FirstPostCreatedTime = root. get("FirstPostCreatedTime"), we are using path due to various other conditions and using criteria builder. Pls assume first post created time and account created time columns are in the same.table.

          – sankar singh
          Jan 20 at 14:26













          If you use DATEDIFF, it will work.

          – Vlad Mihalcea
          Jan 20 at 18:47





          If you use DATEDIFF, it will work.

          – Vlad Mihalcea
          Jan 20 at 18:47













          Yes, I tried, like this. MS Sql Server ' String functionName="Datediff" Predicate predicate=cb.greaterThan(builder.function(functionName,Integer.class,builder.literal("MINUTE"),PostEnteredDateTM, accountCreatedTM), 60 );' Error is : Invalid parameter 1 specified for datediff.

          – sankar singh
          Jan 21 at 19:02







          Yes, I tried, like this. MS Sql Server ' String functionName="Datediff" Predicate predicate=cb.greaterThan(builder.function(functionName,Integer.class,builder.literal("MINUTE"),PostEnteredDateTM, accountCreatedTM), 60 );' Error is : Invalid parameter 1 specified for datediff.

          – sankar singh
          Jan 21 at 19:02




















          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%2f54273310%2fhow-to-fetch-an-entity-having-a-timestamp-property-that-is-within-an-hour-from-o%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

          Callistus III

          Ostreoida

          Plistias Cous