Searching by current date in MYSQL using JPA repository - java

I am using JPA Repository.
I need to search records by ID and Current Date.
Presently I am using
List<Allocation> findByIdAndDate(String Id , Date date);
I am sending current date at the time of method calling
Just like we have method
List<Entity> findByIdAndFlagTrue(String Id)
which by default searches for a true flag,
So if there is any method of query building which searches for current date

Related

Spring jpa check if record exist using String date

I would like to check the DB if a record already exists for a give run date and name using Spring JPA query using two params:
#Query("SELECT CASE WHEN COUNT(r)> 0 THEN true ELSE false END FROM REQUEST r WHERE r.NAME = :reqName AND r.RUN_DATE = :runDate")
boolean existsRequest(#Param("reqName") String reqName,
#Param("runDate") String runDate);
The type of RUN_DATE in Database H2, Sql Server is a DATE field. Yet I am providing String as param for the date in format YYYY-MM-DD.
The above doesn't work for me and I was wondering how to write it correctly using both JPQL and nativeQuery ?
For this specific case, I think the most elegant way is to use query method:
boolean existsByNameAndRunDate(String name, String runDate);

HQL query ignores time portion of the Date object

I have problem with HQL where I am setting the query parameters. One of them is Date. When I debug the code there is Date with time entering the method. I set the parameter using setParameter(timestamp, new Timestamp(date.getTime())) or query.setTimestamp...etc etc I used many combinations...
When I use p6spy to examine the SQL comming from app to the DB there is only '29-Jan-21' or other date without time.
I am using hibernate 5.1.0 final and postgre DB. I'll be glad for any help.
Example:
Query query = getSessionFactory().getCurrentSession().createQuery("SELECT user FROM UserEntity cr WHERE user.userStatus.id = :statusId AND :timestamp >= user.valid_to");
This is how I tried to set the timestamp parameter:
query.setParameter("timestamp", new Timestamp(date.getTime()));
query.setParameter("timestamp", date, TimestampType.INSTANCE);
query.setTimestamp("timestamp", date);
query.setTimestamp("timestamp", new Timestamp(date.getTime()));
Problem is that the generated SQL replace timestamp by '29-Jan-21' or other date I choose but without time. The date parameter comes to the method from UI and it contains full date with time.

MySQL/Hibernate retrieving wrong data with column mapped as LocalTime

I'm mapping a TIME column of a MySQL database in Hibernate (5.4.6.Final) as a java.time.LocalTime class.
This is the declaration of the mapping into the entity:
#Column(name = "TIMEVAL")
public LocalTime TIMEVAL;
The column in the database has value 00:30:00, however Hibernate it's building the instance of TIMEVAL with a value of 01:30:00.
I thought about a difference of time-zones (Ignoring the fact that the TIME data-type doesn't have a time-zone) between the server and the hibernate connection, but I setup each connection and the server as UTC.
And by executing the queries:
SELECT ##global.time_zone;
SELECT ##session.time_zone;
The result of the Hibernate session and the server were all +00:00.
Following the Hibernate configuration:
spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.datasource.url=jdbc:mysql://localhost:3306/..?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&allowMultiQueries=true
And here's the MySQL configuration:
default-time-zone=+00:00
I tried also:
Specifying the MySQL column as VARCHAR(8), but Hibernate always retrieves 01:30:00;
Changing the Entity data-type to Duration, but Hibernate throws an error saying that it can't map TIME to java.lang.Long.
What do I need to modify in order to have Hibernate build the LocalTime instance with the value of the database column?
As #Andreas pointed out in the comments. The JVM time zone wasn't in UTC.
I needed to change the JVM time zone to get the correct value out of the database.
So, by adding the following param to the JVM options:
-Duser.timezone=UTC
Finally the LocalTime instance was built with the value of 00:30:00 as the value of the database column.

Insert Date in Access DB - zero date

I'm making a library project in Java that is supposed to record data about members, books, users and borrowings. When I create a new object of the class Borrowing the system automatically notes the IssueDate.
The problem happens when I try to make a record of the book being returned with and UPDATE SQL QUERY and record the ReturnDate.The query works but in DB in the corresponding field I always get 30/12/1899 as a ReturnDate.
The SQL query goes like this:
UPDATE Booking SET Returned = 1, ReturnDate = "+ sdf.format(getReturnDate()) +" WHERE BookingID =" + getBookingID()
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
setReturnDate(new Date());
I decided to format it to String and format dd/mm/yyyy because that is the format Access recognizes when I enter it manually and it worked with all other queries (like creating a new Booking and IssueDate) in the project and it enters the real date.
I have already tried using java.sql.Date instead, switching to the US format (mm/dd/yyyy) and using # (hash marks) at the beginning and the end of the date string and none of those worked.
Do you have any other ideas?
Consider using a PreparedStatement instead, see Using Prepared Statements for more details
Instead of setting the "text" of the query, use setDate of the PreparedStatement and let the JDBC driver work it out

JPQL to query java.util.Date field of entity using date portion of date time

I have an entity with a date field (java.util.Date). Normally the date is saved as for example 2012-10-19 21:29:03.000. My database is MySQL.
Now I need to query the database, through JPQL, using strictly the date portion, i.e., 2012-10-19. How would I do that?
That can be done by giving TemporalType when setting parameter:
Date param ...
Query q = em.createQuery("SELECT a FROM EntityA a WHERE a.someDate > :param");
q.setParameter("param", param, TemporalType.DATE);

Categories

Resources