How can I avoid the Warning “firstResult/maxResults specified with collection fetch; applying in memory!” when using Hibernate?

Although you are getting valid results, the SQL query fetches all data and it’s not as efficient as it should. So, you have two options. Fixing the issue with two SQL queries that can fetch entities in read-write mode The easiest way to fix this issue is to execute two queries: . The first query … Read more

How to use multiple JOIN FETCH in one JPQL query

Considering we have the following entities: And, you want to fetch some parent Post entities along with all the associated comments and tags collections. If you are using more than one JOIN FETCH directives: List<Post> posts = entityManager.createQuery(“”” select p from Post p left join fetch p.comments left join fetch p.tags where p.id between :minId … Read more

What’s the difference between the IN and MEMBER OF JPQL operators?

IN tests is value of single valued path expression (persistent attribute of your entity) in values you provided to query (or fetched via subquery). MEMBER OF tests is value you provided to query (or defined with expression) member of values in some collection in your entity. Lets’s use following example entity: @Entity public class EntityA … Read more

Limit number of results in JPQL

You can try like this giving 10 results to be fetched explicitly. entityManager.createQuery(JPQL_QUERY) .setParameter(arg0, arg1) .setMaxResults(10) .getResultList(); It will automatically create native query in back-end to retrieve specific number of results, if the backend supports it, and otherwise do the limit in memory after getting all results.

JPQL Like Case Insensitive

You can use the concat operator: @Query(“select u from User u where lower(u.name) like lower(concat(‘%’, ?1,’%’))”) public List<User> findByNameFree(String name); or with a named parameter: @Query(“select u from User u where lower(u.name) like lower(concat(‘%’, :nameToFind,’%’))”) public List<User> findByNameFree(@Param(“nameToFind”) String name); (Tested with Spring Boot 1.4.3)

org.hibernate.NonUniqueResultException: query did not return a unique result: 2?

It seems like your query returns more than one result check the database. In documentation of query.uniqueResult() you can read: Throws: org.hibernate.NonUniqueResultException – if there is more than one matching result If you want to avoid this error and still use unique result request, you can use this kind of workaround query.setMaxResults(1).uniqueResult();

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)