How to fetch an entity having a timestamp property that is within an hour from other timestamp property with...
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:
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)
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
add a comment |
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:
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)
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
add a comment |
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:
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)
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
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:
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)
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
hibernate jpa spring-data-jpa jpa-2.1 hibernate-jpa
edited Jan 20 at 14:16
Vlad Mihalcea
57.5k13156461
57.5k13156461
asked Jan 20 at 3:18
sankar singhsankar singh
6
6
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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 withgreaterThanOrEqualTo, 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 useDATEDIFF, 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 withgreaterThanOrEqualTo, 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 useDATEDIFF, 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
add a comment |
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.
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 withgreaterThanOrEqualTo, 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 useDATEDIFF, 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
add a comment |
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.
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.
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 withgreaterThanOrEqualTo, 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 useDATEDIFF, 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
add a comment |
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 withgreaterThanOrEqualTo, 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 useDATEDIFF, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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