I have the following query:
public List<SomeEntity> findArticlesById(String id) {
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<SomeEntity> criteriaQuery = criteriaBuilder.createQuery(SomeEntity.class);
final Root<SomeEntity> root = criteriaQuery.from(SomeEntity.class);
final Join<SomeEntity, SomeOtherEntity> join = root.join(SomeEntity.property);
criteriaQuery
.select(root)
.where(
criteriaBuilder.equal(join.get(SomeEntity_.id), id),
criteriaBuilder.between(criteriaBuilder.literal(new Date()), join.get(SomeEntity_.startDate), join.get(SomeEntity_.endDate))
);
return entityManager.createQuery(criteriaQuery)
.setHint(QueryHints.HINT_CACHEABLE, true)
.setHint(QueryHints.HINT_CACHE_REGION, CacheRegions.QUERY_ENTITY)
.getResultList();
}
I'd expect the query cache to be hit when I call twice, however it doesn't work.
However, if I remove 2nd predicate
criteriaBuilder.between(criteriaBuilder.literal(new Date()), join.get(SomeEntity_.startDate), join.get(SomeEntity_.endDate)
cache work properly instead.
What is the cause ?
EDIT
class definition:
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Entity
@Table(name = "SOME_ENTITY")
public class SomeEntity implements Serializable {
@EmbeddedId
private SomeEntityPk pk;
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@LazyToOne(LazyToOneOption.NO_PROXY)
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID", referencedColumnName="ID", insertable = false, updatable = false)
private SomeOtherEntity someOtherEntity;
...
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Entity
@Table(name = "SOME_ENTITY")
public class SomeEntity implements Serializable {
@EmbeddedId
private SomeEntityPk pk;
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@LazyToOne(LazyToOneOption.NO_PROXY)
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ID", referencedColumnName="ID", insertable = false, updatable = false)
private SomeOtherEntity someOtherEntity;
...
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@Entity
@Table(name="SOME_OTHER_ENTITY")
public class SomeOtherEntity implements Serializable {
@Id
@Column(name="ID")
private String id;
@Column(name="DATE_START")
@Temporal(TemporalType.TIMESTAMP)
private Date startDate;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="DATE_END")
private Date endDate;
@LazyToOne(LazyToOneOption.NO_PROXY)
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@OneToMany(fetch=FetchType.LAZY, mappedBy=SomeEntity_.SOMEOTHERENTITY)
private List<SomeEntity> someEntities;
...
I'm using Hibernate 5.3.12.Final
new Date()
in the criteria builder and as a result parameter
s bound to the query is different every time. criteriaBuilder.between(criteriaBuilder.literal(new Date())...
findArticlesById
method to findArticlesById(String id, Date date)
and pass the same date
to both calls and see