Date compare time is AM or PM-Hql query - java

In SQL I have a query like this..
SELECT * FROM userdata where date_format(startdate,'%p') = 'AM'
How can I change this query to HQL? For a data range I need only morning data.

date_format should work in hql as well. Try the same query in hql.

Related

how to create predicate of the jpa specification or criteria query for the below query

I am trying to create JPA specification for with filter data between two dates date1 and date2, I tried to create through subquery but no luck as of now, please help if possible.
I am trying to create the specification for the below SQL query,
select
* from
subscription sub join (
select subscription_id,
max(output_date) as max_output_date
from
subscription_package
where
package_status = 'COMPLETE'
group by
subscription_id) sp on
sub.id = sp.subscription_id where
max_output_date between 'date1' and 'date2'
Below is the query created in JPA:
Subquery<Date> datequery = query
.subquery(Date.class);
Root<SubscriptionPackage> dateRoot = datequery
.from(SubscriptionPackage.class);
Subquery<SubscriptionPackage> subquery = query
.subquery(SubscriptionPackage.class);
Root<SubscriptionPackage> subqueryRoot = subquery
.from(SubscriptionPackage.class);
datequery.select(criteriaBuilder
.greatest(subqueryRoot.<Date>get("outputDate")));
datequery.where(criteriaBuilder.equal(subqueryRoot.get("PackageStatus"),
'COMPLETE'));
datequery.groupBy(subqueryRoot.get("subscription"));
not able to use the result from date query for the comparison like
query.where(criteriaBuilder.between(datequery., outFrom, outTo));
I don't know why you are using Subquery and you can use simple Spring data jpa with joins get your desire result.
Please check these examples
For date comparison:
criteriaBuilder.between(root.get("createdDate"), fromDate, toDate));

how to write the sql queries to hibernate

id name date
1. ask 2018-04-25 12:30:59
2. msk 2018-04-25 12:40:43
3. sdf 2017-05-25 12:42:34
id=int---->in java id-->int
name=varchar(25)----> in java name-->string
date=datetime------->in java date--->Timestamp
my sql query=select * from table where year(date)='2018';
o/p:1. ask 2018-04-25 12:30:59
2. msk 2018-04-25 12:40:43
select * from table where month(date)='05'
o/p:3. sdf 2017-05-25 12:42:34
please help me i dont know
how to write this query in hibernate
How to write the above queries in hibernate? I have tried lot things but I didn't get any solution. If I use to_char() in hibernate it will give a unexpected token error.
You do not have to use to_char() function. Hibernate supports year and date functions.
Refer this link: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html
and look under "14.10. Expressions".
So your hql would simply look like this:
select t from table t where YEAR(t.date)='2018';
and select t from table t where MONTH(t.date)='05';
there is no need to use to_char() method here. you can simply use this query inside your Dao Implementation class.
for years:
session.createSQLQuery("select * from table where year(date) = :year")
.setParameter("year",2018)
.list();
for Months
session.createSQLQuery("select * from table where month(date) = :month")
.setParameter("month",05)
.list();

How can i use this sql query to hibernate criteria function

How can i use this sql query to hibernate criteria select * from DailyExpense where month(tdate)=07 and year(tdate)=2016 Daily expense is my pojo class and tdate is column name. i want to select current month data only how to select based on month only.
Use HQL.
from DailyExpense where year(birthDate)=:year and month(birthDate)=:month"
Afterwards, bind :year and :month parameters using query's setString(whetever value of your month and year)

How to use to_char function in JPA to get records from a timestamp

I am try to query from and database using eclipse link using entities. when I use normal query records are returned from current date by when I use JPA zero records are returned. Where could I be doing wrong, Or how can I use to_char function in JPA my database is postgresql.
normal sql query that returns records
select *
from mytable
where to_char(transaction_date, 'YYYY-MM-DD') = '2016-01-19';
MyEntity
#Temporal(javax.persistence.TemporalType.DATE)
private Date transaction_date;
EntityManager e = EntityMgr.MyFact().createEntityManager();
e.getTransaction().begin();
Query qry = e.createQuery("from MyEntity u where u.transaction_date=?1");
qry.setParameter(1, new Date(), TemporalType.DATE);
Have you tried with qry.setParameter(1, new Date(), TemporalType.TIMESTAMP);
You can use to_char function using operator
like
OPERATOR('ToChar', transaction_date,'YYYY-MM-DD')= '2016-01-19';

Hibernate SQL to HQL

Im running into some problems when trying to convert a SQL query into HQL (or Criteria/Restriction). I have the following SQL query:
Select count(n), CreatedDate from (
Select count(serverId) as n, Date(Created) as CreatedDate
from mytable
group by Date(Created), serverId
) as tbl
group by CreatedDate;
So what is the HQL (or Criteria) equivalent?
Some SQL queries cannot be directly translated to HSQL but this might help: HQL Subqueries in joins

Categories