How To Cache DTO Projections With Hibernate
How To Cache DTO Projections With Hibernate
www.thoughts-on-java.org
How to Cache DTO Projections with Hibernate
another article, and I explain it in great details in the Hibernate
Performance Tuning Online Training.
<persistence>
<persistence-unit name="my-persistence-unit">
<description>Hibernate Performance Tuning</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
www.thoughts-on-java.org
How to Cache DTO Projections with Hibernate
The following query uses a constructor expression and selects the
title attribute of the Book entity and the name attribute of the
Publisher entity. For each record returned by this query, Hibernate
calls the constructor of the BookPublisherValue class. In this
example, I use JPA’s Query interface and activate the query cache
with a query hint.
TypedQuery<BookPublisherValue> q = em
.createQuery(
"SELECT new org.thoughts.on.java.model.BookPublisherValue(b.title,
p.name) FROM Book b JOIN b.publisher p WHERE b.id = :id",
BookPublisherValue.class);
q.setHint(QueryHints.CACHEABLE, true);
q.setParameter("id", 1L);
BookPublisherValue value = q.getSingleResult();
www.thoughts-on-java.org