I am using the criteria query as follows:I want to fetch the reservations after the time and date I have passed.but now i am getting the reservation before the time which i have passed for the same date.Please help..
#GET
#Path("/getReservationByUserIdAndTypeAndDateTime/{userid}/{type}/{date}/{time}")
public List<Reservation> getReservationByAndUserIdAndTypeAndDateTime(#PathParam("userid") int uid, #PathParam("type") int tid,#PathParam("date") Date date,#PathParam("time") Time time) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Reservation> criteria = builder.createQuery(Reservation.class);
Root<Reservation> r = criteria.from(Reservation.class);
TypedQuery<Reservation> query = em.createQuery(
criteria.select(r).where(new Predicate[]{
builder.equal(r.get(Reservation_.usersId), uid),
builder.equal(r.get(Reservation_.reservationsType), tid),
builder.greaterThanOrEqualTo(builder.concat(r.get(Reservation_.date), " "+r.get(Reservation_.time)), date.toString()+" "+time.toString())}));
return query.getResultList();
}
Comparing dates and times formatted as string is not advisable, results can depend on the format. It would be better having a unique Date field in the Reservation class:
#Temporal(TIMESTAMP) Date date;
and using date comparison in the query:
builder.greaterThanOrEqualTo(r.get(Reservation_.date), date)
This way the time parameter in the getReservationByAndUserIdAndTypeAndDateTime method can be omitted, since the date parameter would hold both date and time.
Related
I want to return a list of Documents that have a timestamp for a specific date. However, the timestamp also contains the time. Is there a method to filter by just the date part of the timestamp?
timestamp: 2022-10-27T11:11:07.350+00:00
Query method:
public static Query getEventByDateQuery(final String date) {
return Query
.query(
where("timestamp").......
);
}
My domain has and Date field updated, that I want to search by
#Column(name = "updated")
Date updated;
I have a Java Date object representing a day, that's passed in by my endpoint's controller.
Date day;
and a crudrepository representing my data
public interface DataRepository extends CrudRepository<Data, Long> {
List<Data> findByLastUpdatedInDate(Date date);
}
Obviously the above method doesn't work, but is there something similar? Or am I going to have to do a find between then manually search for the last entry?
Edit: Here's how I get day; dateString is passed in by the controller.
SimpleDateFormat dateFormatIn = new SimpleDateFormat("yyyy-MM-dd");
Date day = dateFormatIn.parse(dateString);
You're almost there. The slight tweak would be this if I read the documentation correctly:
Data data = dataRepository.findTopByUpdatedBetweenOrderByUpdatedDesc(start, stop);
Source: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.limit-query-result
You can annotatw your method with #Query annotation and write HQL query
#Query("select d from Data d where d.updated= :date")
List<Data> findByLastUpdatedInDate(#Param("date") Date date);
I'm hoping someone finds something more elegant, but for now what I'm using is
List<Data> findByUpdatedBetweenOrderByUpdatedDesc(Date start, Date stop);
And converting day with
Date start = new Date(day.getTime());
Date stop = new Date(day.getTime() + 86400000L);
to get my entry with
Data data = dataRepository.findByUpdatedBetweenOrderByUpdatedDesc(start, stop).get(0);
I would like to compare date in Hibernate query. In my database data is in TIMESTAMP but my input in string so I would like to convert TIMESTAMP to string and compare my date.
List<User> usersList = session.createQuery("from User where CAST(DATE(created_date) as CHAR)=2015-01-19").list();
This is working in MySQL but not in Hibernate, please give me solution for this issue.
You can use SimpleDateFormat here
First create Date object from timestamp
Date createdDateTemp= new Date(timestamMillis);
Using SimpleDateFormat convert date in to formated string
String createdDateString = new SimpleDateFormat("yyyy-MM-dd").format(createdDateTemp));
now your HQL query will look like below
List<User> usersList=session.createQuery("from User where :createdDate = 2015-01-19").setParameter("createdDate", createdDateString).list();
I have stored the date in my database (not months).i want to get the records from particular month.Is it possible to write criteria for this in hibernate3?
You can write like this
Date startDate = 2014-03-01
Date endDate = 2014-03-31
Criteria criteria = getSession().createCriteria(getDomainClass());
criteria.add(Restrictions.between("date", startDate, endDate));
(OR)
If your date format is "YYYY-MM-DD"
String date = "2014-03";
Criteria criteria = getSession().createCriteria(getDomainClass());
criteria.add(Restrictions.like("date", date));
I've the following hql query:
Query query = session.createQuery("from Appointments where datetime < :now");
query.setDate("now", new Date());
listApps = query.list();
It return only the records before today (yesterday, 2 days ago and so on)
But not the record of today before than this time.
The datetime into the database is of type DateTime, so it contains both date and time.
Why is this query not returning the record of today before than the current time?
This is the way datetime is defined inside my entity:
#Temporal(TemporalType.TIMESTAMP)
private Date datetime;
Change from
query.setDate("now", new Date());
to
query.setTimestamp("now", new Date());
or to:
query.setParameter("now", new Date());
Bind the date (time is truncated) of a given Date object to a named query parameter.
public Query setDate(String name, Date date);
Bind the date and the time of a given Date object to a named query parameter.
public Query setTimestamp(String name, Date date);