Created
May 12, 2022 13:29
-
-
Save jpukg/39d05856da764fa38814211bb6e7743b to your computer and use it in GitHub Desktop.
Getting large number of records from database without having primary cache + Separate entity manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Employee> findAllEmployee(int offset, int limit) { | |
String employeeQuery = String.format("select distinct r from %s r left join r.owner as u", Employee.class); | |
employeeQuery += " WHERE r.status = :status ORDER BY r.lastModification DESC"; | |
EntityManagerFactory factory = Persistence.createEntityManagerFactory("employee_ds"); | |
EntityManager shortLiveEntityManager = factory.createEntityManager(); | |
TypedQuery<RISIndex> query = shortLiveEntityManager.createQuery(employeeQuery, Employee.class).setFlushMode(FlushModeType.COMMIT);; | |
query.setParameter("status", Status.APPROVED); | |
query.setHint(QueryHints.QUERY_RESULTS_CACHE, false); | |
query.setHint(QueryHints.CACHE_USAGE, CacheUsage.CheckCacheThenDatabase); | |
List<RISIndex> resultList = query | |
.setFirstResult(offset) // offset | |
.setMaxResults(limit + offset) // limit | |
.getResultList(); | |
shortLiveEntityManager.close(); | |
return resultList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Annotate with TransactionManagementType.BEAN , so that the container (Weblogic, etc) timeout will not be occurred (default timeout of 30 seconds)