I have a scenario where i use a custom user type for a entity field in hibernate using the org.hibernate.usertype.UserType.
It's a date conversion to UTC
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
public final class UTCTimestampType implements ParameterizedType, UserType
{..}
My entity class is like the below
@Entity
public class Person extends AbstractPerson
{
@Column(name = "BIRTH_DT_TM")
@Type(type = "com.myexample.hibernate.types.UTCTimestampType")
protected Date birthDtTm;
}
I have two queries to retreive person from Db when current time is greater than brith date
1)One in jpql, which actually calls the UserType#nullSafeSet as expected
2) but if i build the same query via criteria builder the UserType#nullSafeSet implementation in the class UTCTimestampType is never get called and the query just executes.
Am not sure why this happens
this i my sample unit test for case (1)
String query = "SELECT pFROM Person p where p.birthDtTm = :now";
Query q = entityManager.createQuery(query);
q.setParameter("now", new Date());
List<Person> persons= q.getResultList();
But my criteria query doesn't go through my custom UserType class
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
Predicate predicate = criteriaBuilder.greaterThanOrEqualTo(
root.get(Person_.birthDtTm), new Date());
criteriaQuery.where(predicate);
criteriaQuery.select(root);
TypedQuery<Person> q = entityManager.createQuery(criteriaQuery2);
List<Person> persons = q.getResultList();
I want the birth date field to be converted to UTC time zone before my query is executed, this worked in jpql query, but in criteria builder the custom type is never called and the query executes with the passed in time. My end sql statment should be Ex:SELECT * FROM Person p where p.birthDtTm = date;
where date is in UTC time zone